触摸结束未被调用

我正在努力识别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 = NO;设置recognizer.cancelsTouchesInView = false recognizer.cancelsTouchesInView = NO; 在Objective-C中

尝试删除该视图上的任何手势识别器。 他们可以干扰touchesEnded

如果不调用super的实现,则必须覆盖所有触摸方法。 所以你还必须实现touchesMoved:withEvent:touchesCancelled:withEvent:方法。 实现可以是空的,但您必须这样做。

touchesBegan:withEvent:

如果在不调用super(常用模式)的情况下覆盖此方法,则还必须覆盖其他方法来处理触摸事件,如果仅作为存根(空)实现。

基于UIResponder类参考

尝试覆盖UIViewhitTest方法:

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { return self; } 

有可能当你举起手指时,它们不会被识别为在UIView ,因此不会调用touchesEnded

我有同样的问题,并在结束的触摸中用这个简单的代码解决了它:

 NSInteger touchCount = 0; for (UITouch *touch in touches) { touchCount++; if([[event allTouches]count] == touchCount){ // do what you have to do here } } 

//你会在这里收到警告,但不关心它

我希望这有帮助!

即使在定义了所有四个触摸处理程序之后,我也遇到了同样的问题。 我也没有任何主动识别器。

我的解决方案是在我的视图上简单地启用多点触摸事件,就像这样

 self.multipleTouchEnabled = YES; 

这神奇地解决了这个问题。

请注意,在此之前我确实获得了“多点触摸”事件,即同时针对多个手指的事件,但我有上述问题,并非所有事件都正确结束。

希望有人帮助! 🙂