我目前正在开发基于3D OpenGLES的iPhone游戏项目,我希望有一个可以检测触摸的视图,以便我可以根据触摸手势旋转我的相机。
所以在这里我创建了一个TouchPad类,它派生自UIView来记录触摸手势。 完成后我发现只有一半的屏幕有效。整个左侧。 每当我触摸屏幕的右侧部分时,它都不会调用 - [touchesBegan]方法
这是我的班级定义
@interface TouchPad : UIView
{
CGPoint fingerMove, originalPosition;
}
@property (nonatomic, assign) CGPoint fingerMove;
@end
这是我的课程实施
#import "TouchPad.h"
@implementation TouchPad
@synthesize fingerMove;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
fingerMove = CGPointMake(0, 0);
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
std::cout << "called";
UITouch *touch = [touches anyObject];
originalPosition = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
fingerMove = CGPointMake(currentPosition.x - originalPosition.x,
currentPosition.y - originalPosition.y);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
{
UITouch *touch = [touches anyObject];
originalPosition = [touch locationInView:self];
fingerMove = CGPointMake(0, 0);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
fingerMove = CGPointMake(0, 0);
}
@end
以下是我在ViewController中使用它的方法
- (void)prepareGame
{
game = [[MainGame alloc] init];
[self.view addSubview:game.IrrView];
[game.IrrView release];
rotationPad = [[TouchPad alloc] initWithFrame:CGRectMake(0.0, 0.0,
[[UIScreen mainScreen] bounds].size.width,
[[UIScreen mainScreen] bounds].size.height)];
[self.view addSubview:rotationPad];
[rotationPad release];
}
- (void)runGame
{
[game updateMapPattern];
[game updateCamera :rotationPad.fingerMove];
[game draw];
}
我真的想知道如何让触摸板在整个屏幕上工作,而不仅仅是屏幕的左侧部分。也许与我的游戏视图存在冲突