UITapGestureRecognizer - 单击和双击
我正在尝试向视图添加 2 个 UITapGestureRecognizers,一个用于单击事件,一个用于双击事件。单击识别器按预期工作(自行)。但我似乎无法让双击识别器工作。
尝试过使用以下属性进行实验:cancelsTouchesInView
、delaysTouchesBegan
和 delaysTouchesEnded
但仍然不起作用。
当我双击时,单击识别器将始终被激活,并且双击事件也会发送到超级视图。但自定义的双击识别器似乎根本没有收到通知。
文档似乎表明上述 3 个属性可以用于此目的。但我只是不确定应该设置什么值以及哪个识别器(单、双或两者)。希望熟悉这方面的人可以帮忙。
以下是最新更新的代码块。
// ****** gesture recognizers ******
- (void)addSingleAndDoubleTapGestureRecognizersToView:(UIView *)view
{
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer: singleTapRecognizer];
// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
[view addGestureRecognizer: doubleTapRecognizer];
}
- (void)handleSingleTapOnView:(id)sender
{
}
- (void)handleDoubleTapOnView:(id)sender
{
}
I am trying to add 2 UITapGestureRecognizers
to a view, one for single tap and one for double tap events. The single tap recognizer is working as expected (on its own). But I don't seem to be able to get the double tap recognizer working.
Have tried to experiment with properties like : cancelsTouchesInView
, delaysTouchesBegan
and delaysTouchesEnded
but still doesn't work.
When I double tap, the single tap recognizer would always be activated and the double tap event would also be sent to the super view. But the custom double tap recognizer does not seem to be notified at all.
Documentations seem to suggest that the 3 properties mentioned above could be used for the purpose. But I am just not sure what values should be set and on which recognizer(s) (single, double or both). Hope somebody familiar with this could help.
The following is the latest updated code block.
// ****** gesture recognizers ******
- (void)addSingleAndDoubleTapGestureRecognizersToView:(UIView *)view
{
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer: singleTapRecognizer];
// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
[view addGestureRecognizer: doubleTapRecognizer];
}
- (void)handleSingleTapOnView:(id)sender
{
}
- (void)handleDoubleTapOnView:(id)sender
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
注意: 如果您使用
numberOfTouchesRequired
它必须是。 numberOfTouchesRequired = 1;
对于 Swift
Note: If you are using
numberOfTouchesRequired
it has to be.numberOfTouchesRequired = 1;
For Swift
Swift 3 解决方案:
在代码行 singleTap.require(toFail: doubleTap) 中,我们强制单击等待并确保点击事件不是双击。
Swift 3 solution:
In the code line singleTap.require(toFail: doubleTap) we are forcing the single tap to wait and ensure that the tap event is not a double tap.
您需要使用 requireGestureRecognizerToFail: 方法。像这样的东西:
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
You need to use the requireGestureRecognizerToFail: method. Something like this:
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
//----首先你必须分配双击和单击手势-------//
//------------------------ -点击次数----------------//
//-------- 在视图或按钮上添加双击和单击手势-------- -//
//----firstly you have to alloc the double and single tap gesture-------//
//-----------------------number of tap----------------//
//------- add double tap and single tap gesture on the view or button--------//
不确定这是否正是您想要的,但我在没有手势识别器的情况下进行了单击/双击。我在 UITableView 中使用它,所以我在 didSelectRowAtIndexPath 方法中使用了该代码
方法 singleTap 和 doubleTap 只是以 NSIndexPath 作为参数为空:
希望它有帮助
Not sure if that's exactly what are you looking for, but I did single/double taps without gesture recognizers. I'm using it in a UITableView, so I used that code in the didSelectRowAtIndexPath method
Methods singleTap and doubleTap are just void with NSIndexPath as a parameter:
Hope it helps
Swift 2 的解决方案:
Solution for Swift 2:
我实现了 UIGestureRecognizerDelegate 方法来检测 singleTap 和 doubleTap。
就这样做吧。
然后实现这些委托方法。
I implemented UIGestureRecognizerDelegate methods to detect both singleTap and doubleTap.
Just do this .
Then implement these delegate methods.
有些视图有自己的内置双击识别器(
MKMapView
就是一个例子)。要解决这个问题,您需要实现UIGestureRecognizerDelegate
方法shouldRecognizeSimultaneouslyWithGestureRecognizer
并返回YES
:首先实现双识别器和单识别器:
然后实现:
Some view have there own double tap recognizers built in (
MKMapView
being an example). To get around this you will need to implementUIGestureRecognizerDelegate
methodshouldRecognizeSimultaneouslyWithGestureRecognizer
and returnYES
:First implement your double and single recognizers:
And then implement:
参考 @stanley 的评论 -
不要尝试使用点击手势来选择行以在
UITableView
中工作,因为它已经具有完整的点击处理......但您必须设置“取消视图中的触摸”在单击手势识别器上设置为“否”,否则它将永远不会获得点击事件。
In reference to @stanley's comment -
Don't try and use tap gestures to row selections to work in
UITableView
as it already has full tap handling....But you must set 'Cancels Touches in View' to 'NO' on your single tap gesture recognizers or it will never get the tap events.