检测手指向上/向下 UITapGestureRecognizer

发布于 2024-12-19 11:39:31 字数 490 浏览 1 评论 0原文

如何使用 UITapGestureRecognizer 知道手指何时放下以及何时抬起?
文档说我应该只处理UIGestureRecognizerStateEnded 作为点击,这意味着当手指按下时有 UIGestureRecognizerStateBegin ,但我得到的只是UIGestureRecognizerStateEnded
我用来注册识别器的代码是:

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]

How can I know when the finger is down and when is it up with UITapGestureRecognizer?
The documentation says I should only handle UIGestureRecognizerStateEnded as tap so it means there is UIGestureRecognizerStateBegin when finger is down, but all I get is UIGestureRecognizerStateEnded.
The code I use to register the recognizer is:

[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月打碎记忆 2024-12-26 11:39:31

UITapGestureRecognizer 是一个离散手势识别器,因此永远不会转换到开始或更改状态。来自 UIGestureRecognizer 类参考:

离散手势从“可能”过渡到“已识别”
(UIGestureRecognizerStateRecognized) 或失败
(UIGestureRecognizerStateFailed),取决于它们是否
是否成功解释手势。如果手势识别器
转换为“已识别”,它将其操作消息发送到其目标。

(当然要记住 UIGestureRecognizerStateRecognized == UIGestureRecognizerStateEnded)。

文档说,在您触发代码以表明它已被识别之前,您应该检查点击手势识别器的状态以查看它是否处于结束状态。他们并没有说点击手势实际上会转换到开始或更改的状态(尽管我承认文档在使用的语言上有点误导!)。

如果您想检查点击手势识别器的手指按下事件,我建议仅使用 touchesBegan:withEvent:,因为无论如何这才是您真正想要的。

UITapGestureRecognizer is a discrete gesture recognizer, and therefore never transitions to the began or changed states. From the UIGestureRecognizer Class Reference:

Discrete gestures transition from Possible to either Recognized
(UIGestureRecognizerStateRecognized) or Failed
(UIGestureRecognizerStateFailed), depending on whether they
successfully interpret the gesture or not. If the gesture recognizer
transitions to Recognized, it sends its action message to its target.

(Remembering of course that UIGestureRecognizerStateRecognized == UIGestureRecognizerStateEnded).

The docs are saying that you should check the state of a tap gesture recognizer to see that it is in its ended state, before you fire your code to say that it has been recognized. They are not saying that the tap gesture actually transitions to the began or changed states (although I admit that the docs are a little misleading in the language used!).

If you want to check for the finger down event for a tap gesture recognizer, I would recommend just using touchesBegan:withEvent:, since this is what you are really after anyway.

顾铮苏瑾 2024-12-26 11:39:31

您可以重写委托方法 -(BOOL)gestureRecognizer:shouldReceiveTouch:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"Hello from press down");

    return YES;
}

You could override the delegate method -(BOOL)gestureRecognizer:shouldReceiveTouch:.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"Hello from press down");

    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文