我目前正在开发一个基于 3D OpenGLES 的 iPhone 游戏项目,我想要一个检测触摸的视图,这样我就可以根据触摸手势旋转我的相机。
所以在这里我创建了一个从 UIView 派生的 TouchPad 类来记录触摸手势。完成后,我发现只有一半的屏幕有效。整个左侧。每当我触摸屏幕的右侧时,它都不会调用 -[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];
}
我真的很想知道如何让触摸板在整个屏幕上工作,而不仅仅是屏幕的左侧。可能和我的游戏观有冲突