使用 UIView 只检测双击或单击?
我想在用户触摸视图时检测到双击/单击。
我做了这样的事情:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint prevLoc = [touch ]
if(touch.tapCount == 2)
NSLog(@"tapCount 2");
else if(touch.tapCount == 1)
NSLog(@"tapCount 1");
}
但它总是在 2 次点击之前检测到 1 次点击。如何检测 1 / 2 次点击?
I want to detect JUST double / single tap when the user touches a view.
I made something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint prevLoc = [touch ]
if(touch.tapCount == 2)
NSLog(@"tapCount 2");
else if(touch.tapCount == 1)
NSLog(@"tapCount 1");
}
But it always detect 1 tap before 2 taps. How can I detect just 1 / 2 tap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谢谢你的帮助。我也找到了类似的方法:
Thanks for you help. I also found a way like that:
这将有助于定义单击和双击的方法,
因此,在
touchesEnded
中,您可以根据点击次数调用适当的方法,但仅在延迟后调用handleSingleTap,以确保双击没有被执行。执行:在
touchesBegan
中,取消所有对handleSingleTap的请求,以便第二次tap取消第一次tap对handleSingleTap
的调用,并且只调用handleDoubleTap
It will help to define methods for single and double taps
So then in
touchesEnded
you can call the appropriate method based on the number of taps, but only call handleSingleTap after a delay to make sure double tap has not been executed:In
touchesBegan
, cancel all requests to handleSingleTap so that the second tap cancels the first tap's call tohandleSingleTap
and onlyhandleDoubleTap
will be called也许你可以使用一些时间间隔。等待 (x) 毫秒调度事件。如果您在该时间段内获得两次点击,请进行双击。如果您只获得一次调度,请点击一下。
Maby you can use some time interval. Wait with dispatching the event for (x)ms. If you get two taps in that time-period, dispatch a double-tap. If you only get one- dispatch single tap.