今天早上我在使用 Cocos2D v1.0.0 时遇到了这个问题。我的解决方案是在该层的 init 方法中包含 CCTouchDispatcher 方法调用,然后在 UIView 中的该层将识别触摸。
-(id) init
{
if ((self = [super init]) != nil) {
// do stuff
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]];
NSLog(@"TouchBegan at x:%0.2f, y:%0.2f", location.x, location.y);
return YES;
}
另一种解决方案是使用 ccTouchesBegan 方法:
-(id) init
{
if ((self = [super init]) != nil) {
// do stuff
self.isTouchEnabled = YES;
}
return self;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *thisTouch in touches) {
CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[thisTouch locationInView:[thisTouch view]]]];
NSLog(@"TouchesBegan at x:%0.2f, y:%0.2f", location.x, location.y);
}
}
请注意,这两种触摸方法有不同的方法让您的应用程序知道它应该响应触摸。您无法混合搭配您想要如何响应触摸,以及您想要观察哪些触摸。