gpt4 book ai didi

ios - UITapGestureRecognizer 不适用于 .began 状态

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:27 25 4
gpt4 key购买 nike

我在我的 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 触发。当我将 tap 设置为 .ended 时,两者都会触发。

为什么在状态设置为 .began 时点击不起作用?

最佳答案

Handling UIKit Gestures告诉我们:

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.

...

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 状态。

(Handling UIKit Gestures 文档告诉我们,离散手势的处理程序将被称为“恰好一次”。这与我的经验一致。令人困惑的是,tap gesture documentationHandling Tap Gestures 引用建议应该测试对于 .ended 状态,尽管在实践中,这是您的点击手势处理程序将被调用的唯一状态。无论如何,寻找 状态可能是明智的.ended,永远不要期望在您的离散手势处理程序中看到 .began 状态。)

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

关于ios - UITapGestureRecognizer 不适用于 .began 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26763997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com