iOS检测用户触摸释放

前端之家收集整理的这篇文章主要介绍了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];
}

任何建议都会很棒,特别是如果有人知道如何实现一个返回用户触摸设备状态的调用.我查看了开发人员文档并且空洞了.

解决方法

经过几个小时的修补,我找到了一种有效的方法,不确定它是否是最好的方法.结果我需要像下面的代码一样编写它.我没有调用我在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];
    }
}
原文链接:https://www.f2er.com/iOS/329989.html

猜你在找的iOS相关文章