2011-09-11 106 views
4

这可能是某处张贴在这里,但我不能找到它。我正在用两个UIViews编写一个简单的iOS应用程序。用户将首先按住屏幕的某个区域,然后释放他们的触摸,然后快速触摸下面的第二个视图。的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]; 
} 

任何建议将是巨大的,尤其是如果有人知道如何实现返回触摸设备的用户的状态的呼叫。我查看了开发人员文档,并且已经空了。

+0

您可以用按钮来代替看法? UIButton有一个TouchUpInside事件。 –

回答

9

摆弄了几个小时后,我发现,工作的方式,不知道如果它是最好的办法。原来我需要像下面的代码那样写它。我没有调用我在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]; 
    } 
} 
+0

伟大的解决方案。我加了'[holdGesture setMinimumPressDuration:0.01];'因为它更加敏感。 – Segev

2

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

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

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

有关触摸操作的更多信息,从2011年WWDC本次会议是金(需要开发帐号):

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

+0

+1为通用方法。 – Jacksonkr