UTapGestureRecognizer 不适用于 .began 状态 [英] UTapGestureRecognizer doesn't work for .began state

查看:17
本文介绍了UTapGestureRecognizer 不适用于 .began 状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIView 中添加了两个手势识别器:

I've added two gesture recognizers to my UIView:

func tap(sender: UITapGestureRecognizer){
    if sender.state == .began {
        print("snapping photo")
    }

}
func longPress(sender: UILongPressGestureRecognizer) {
    if sender.state == .began {
        print("snapping video")
    }
}

当两者都设置为 state == .began 时,只有 longPress 会触发.当我将点击设置为 .ended 时,两者都会触发.

When both are set to state == .began, only longPress fires. When I set tap to .ended, both fire.

为什么当状态设置为 .began 时,tap 不起作用?

Why doesn't tap work when its state is set to .began?

推荐答案

处理 UIKit手势告诉我们:

手势识别器有两种类型:离散和连续.离散手势识别器在识别手势后只调用一次您的操作方法.在满足初始识别标准后,连续手势识别器会多次调用您的操作方法,并在手势事件中的信息发生变化时通知您.

Gesture recognizers come in two types: discrete and continuous. A discrete gesture recognizer calls your action method exactly once after the gesture is recognized. After its initial recognition criteria are met, a continuous gesture recognizer performs calls your action method many times, notifying you whenever the information in the gesture’s event changes.

...

state 属性手势识别器传达对象的当前识别状态.对于连续手势,手势识别器将此属性的值从 .began 更新为 .changed.ended.取消.您的操作方法使用此属性来确定适当的操作过程.

The state property of a gesture recognizer communicates the object’s current state of recognition. For continuous gestures, the gesture recognizer updates the value of this property from .began to .changed to .ended, or to .cancelled. Your action methods use this property to determine an appropriate course of action.

UITapGestureRecognizer 是一个离散的手势,因此,当手势被识别时,您的事件处理程序将被调用一次.在实践中,这意味着您的点击手势识别器处理程序不会在 .began 状态下被调用.然而,它被称为 .ended 状态.

The UITapGestureRecognizer is a discrete gesture, and as such, your event handler is called once when the gesture was recognized. In practice, this means that your tap gesture recognizer handler will not be called for a state of .began. It is, however, called for a state of .ended.

(Handling UIKit Gestures 文档告诉我们离散手势将被称为恰好一次".这与我的经验一致.令人困惑的是,点击手势文档Handling Tap Gestures 参考建议一个应该测试 .ended 的状态,即使在实践中,这是您的点击手势处理程序将被调用的唯一状态.无论如何,虽然寻找 .ended 的状态可能是谨慎的code>.ended,永远不要期望在你的离散手势中看到 .began 的状态 handlers.)

(The Handling UIKit Gestures documentation tells us that the handler of a discrete gesture will be called "exactly once". This is consistent with my experience. Confusingly, the tap gesture documentation and the Handling Tap Gestures reference suggest that one should test for a state of .ended even though, in practice, that’s the only state for which your tap gesture handler will ever be called. Regardless, while it might be prudent to look for a state of .ended, don’t ever expect to ever see a state of .began in your discrete gesture handlers.)

另一方面,UILongPressGestureRecognizer 是一个连续的手势,因此检查状态非常有用(确定手势何时 .began, .changed.ended 等).这就是为什么你看到它调用 .beganstate.

The UILongPressGestureRecognizer, on the other hand, is a continuous gesture, so checking the state is very useful (determining when the gesture .began, .changed, .ended, etc.). That's why you see it called for the state of .began.

这篇关于UTapGestureRecognizer 不适用于 .began 状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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