我一直在尝试识别iOS应用程序中的触摸,并且我有这个简单的代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%lu",(unsigned long)[touches count]);
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
UITouch *touch = obj;
CGPoint touchLocation = [touch locationInNode:self.scene];
NSLog(@"B x:%f - y:%f",touchLocation.x,touchLocation.y);
}];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[touches enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
UITouch *touch = obj;
CGPoint touchLocation = [touch locationInNode:self.scene];
NSLog(@"E x:%f - y:%f",touchLocation.x,touchLocation.y);
}];
}
touchesBegan被称为正常,如果我同时从1根手指到5根手指放在屏幕上,我会看到它以正确的信息被调用
touchesBegan
不会发生相同的情况,很多时候,如果我在屏幕上有3个手指并同时移开它们,我只会看到2次触摸结束的信息(有时甚至是1个)。如果我一次伸出一根手指,该方法通常也会被调用2次(有时为1次,尽管很少会被称为正确的3次)。随着触摸次数的增加,
touchesEnded
方法中未显示某些信息的可能面纱还实现了
touchesMoved:withEvent:
和touchesCancelled:withEvent:
方法,它们的逻辑相同有人可以解释这种行为吗?有什么我想念的吗?
最佳答案
您必须在Swift中设置recognizer.cancelsTouchesInView = false
或在Objective-C中设置recognizer.cancelsTouchesInView = NO;