处理长按结束 [英] Handle long press end

查看:125
本文介绍了处理长按结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 UILongPressGestureRecognizer 附加到控制器的视图。我想冻结一些计时器,直到用户握住他的手指。问题是我无法确定触摸事件何时结束。也许我应该在手势识别器的属性上使用观察者?或者还有其他方法可以做到这一点吗?

I have an UILongPressGestureRecognizer attached to the controller's view. I want to freeze some timers until user holds his finger. The problem is I can't determine when the touch event is ended. Maybe I should use observer on the gesture recognizer's property state? Or there are other ways to do this?

在控制器的视图中放置 UIScrollView (实现分页库),可以通过拖动(滑动)切换页面。还有一个 UITapGestureRecognizer ,也附加到控制器的视图,它可以处理其他一些任务。

On the controller's view a UIScrollView (which implements a paged gallery) is placed, pages can be switched by dragging (swiping). Also there is an UITapGestureRecognizer, also attached to the controller's view, which handles some other tasks.

推荐答案

是的,您可以通过查看识别器的状态来完成此操作,但您不需要使用观察者。您应该在识别器触发时在手势识别器的委托中声明一个操作方法。只要识别器的状态发生变化,就会自动调用该方法。

Yes you can accomplish this by looking at the recognizer's state, but you don't need to use an observer. You should declare an action method in your gesture recognizer's delegate that will be called when the recognizer fires. The method will then automatically be called whenever the recognizer's state changes.

你需要查找状态UIGestureRecognizerStateBegan来启动计时器,你需要查找状态UIGestureRecognizerStateEnded ,UIGestureRecognizerStateFailed和UIGestureRecognizerStateCancelled暂停你的计时器。

You need to look for the state UIGestureRecognizerStateBegan to begin your timer, and you need to look for the states UIGestureRecognizerStateEnded, UIGestureRecognizerStateFailed, and UIGestureRecognizerStateCancelled to pause your timer.

只需将你的手势与Interface Builder中的动作连接起来。

Simply connect up your gesture with the action in Interface Builder.

-(IBAction)longPressBegan:(UILongPressGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {
        // Long press detected, start the timer
    }
    else
    {
        if (recognizer.state == UIGestureRecognizerStateCancelled
            || recognizer.state == UIGestureRecognizerStateFailed
            || recognizer.state == UIGestureRecognizerStateEnded)
        {
            // Long press ended, stop the timer
        }
    }
}

这篇关于处理长按结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆