检测手指向上/向下 UITapGestureRecognizer
如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UITapGestureRecognizer
是一个离散手势识别器,因此永远不会转换到开始或更改状态。来自 UIGestureRecognizer 类参考:(当然要记住
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:(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.您可以重写委托方法
-(BOOL)gestureRecognizer:shouldReceiveTouch:
。You could override the delegate method
-(BOOL)gestureRecognizer:shouldReceiveTouch:
.