iOS检测用户触摸版本

这可能已经张贴在这里某处,但我找不到它。 我正在用两个UIViews写一个简单的iOS应用程序。 用户将首先按住屏幕的某个区域,然后松开触摸,然后快速触摸下面的第二个视图。

第一个UIView有一个UILongPressGestureRecognizer附加到它,工作正常。 第二个UIView有一个UITapGestureRecognizer附加到它,也工作正常。 然而,我不能让这些手势识别器中的任何一个返回说明用户释放触摸的任何东西。

我试过这个代码无济于事:

- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer { if (UIGestureRecognizerStateRecognized) { holdLabel.text = @"Holding Correctly. Release Touch when ready."; holdView.backgroundColor = [UIColor greenColor]; } else if (UIGestureRecognizerStateCancelled){ holdLabel.text = @"Ended"; holdView.backgroundColor = [UIColor redColor]; } 

任何build议将是伟大的,特别是如果有人知道如何实现一个呼叫,返回用户触摸设备的状态。 我查阅了开发者文档,并且已经空了。

经过了几个小时的修补,我发现了一个工作的方式,不知道是否是最好的方法来做到这一点。 原来我需要像下面的代码一样写下来。 我没有调用我在viewDidLoad()方法中声明的特定UIGestureRecognizer。

 - (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer { if (holdRecognizer.state == UIGestureRecognizerStateBegan) { holdLabel.text = @"Holding Correctly. Release when ready."; holdView.backgroundColor = [UIColor greenColor]; } else if (holdRecognizer.state == UIGestureRecognizerStateEnded) { holdLabel.text = @"You let go!"; holdView.backgroundColor = [UIColor redColor]; } } 

您需要在这里使用手动触摸处理(而不是使用手势识别器)。 任何UIResponder子类都可以实现以下四种方法:

 – touchesBegan:withEvent: – touchesMoved:withEvent: – touchesEnded:withEvent: – touchesCancelled:withEvent: 

通过使用这些方法,您可以访问触摸事件的每个阶段。 您可能必须执行自己的逻辑来检测长按,但是您可以完全访问所有触摸。

有关触控处理的更多信息,请参阅WWDC 2011的这一部分内容。

https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296