ios UITapGestureRecognizer - 单击和双击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8876202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITapGestureRecognizer - single tap and double tap
提问by Stanley
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.
我正在尝试向UITapGestureRecognizers
视图添加 2个,一个用于单击,另一个用于双击事件。单击识别器按预期工作(单独运行)。但我似乎无法让双击识别器工作。
Have tried to experiment with properties like : cancelsTouchesInView
, delaysTouchesBegan
and delaysTouchesEnded
but still doesn't work.
已经尝试具有类似性质的实验:cancelsTouchesInView
,delaysTouchesBegan
和delaysTouchesEnded
,但仍然无法正常工作。
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.
文档似乎表明上述 3 个属性可以用于此目的。但我只是不确定应该设置什么值以及在哪个识别器(单个、双重或两者)上设置。希望熟悉这方面的人可以帮忙。
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
{
}
回答by Anil Kothari
UITapGestureRecognizer *singleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSingleTap)] autorelease];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
UITapGestureRecognizer *doubleTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap)] autorelease];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
Note:If you are usingnumberOfTouchesRequired
it has to be.numberOfTouchesRequired = 1;
注意:如果您正在使用numberOfTouchesRequired
它必须是.numberOfTouchesRequired = 1;
For Swift
对于斯威夫特
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didPressPartButton))
singleTapGesture.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
singleTapGesture.require(toFail: doubleTapGesture)
回答by Surya Kameswara Rao Ravi
Swift 3 solution:
斯威夫特 3 解决方案:
let singleTap = UITapGestureRecognizer(target: self, action:#selector(self.singleTapAction(_:)))
singleTap.numberOfTapsRequired = 1
view.addGestureRecognizer(singleTap)
let doubleTap = UITapGestureRecognizer(target: self, action:#selector(self.doubleTapAction(_:)))
doubleTap.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTap)
singleTap.require(toFail: doubleTap)
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.
在代码行singleTap.require(toFail: doubleTap) 中,我们强制单击等待并确保单击事件不是双击。
回答by UIAdam
You need to use the requireGestureRecognizerToFail: method. Something like this:
您需要使用 requireGestureRecognizerToFail: 方法。像这样的东西:
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
回答by Jaspreet Singh
//----firstly you have to alloc the double and single tap gesture-------//
//----首先你必须分配双击和单击手势-------//
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap:)];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap:)];
[singleTap requireGestureRecognizerToFail : doubleTap];
[doubleTap setDelaysTouchesBegan : YES];
[singleTap setDelaysTouchesBegan : YES];
//-----------------------number of tap----------------//
//----------------------点击次数--------------------------------
[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];
//------- add double tap and single tap gesture on the view or button--------//
//------- 在视图或按钮上添加双击和单击手势--------//
[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];
回答by Novarg
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
不确定这是否正是您要寻找的,但我在没有手势识别器的情况下进行了单击/双击。我在 UITableView 中使用它,所以我在 didSelectRowAtIndexPath 方法中使用了该代码
tapCount++;
switch (tapCount)
{
case 1: //single tap
[self performSelector:@selector(singleTap:) withObject: indexPath afterDelay: 0.2];
break;
case 2: //double tap
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap:) object:indexPath];
[self performSelector:@selector(doubleTap:) withObject: indexPath];
break;
default:
break;
}
if (tapCount>2) tapCount=0;
Methods singleTap and doubleTap are just void with NSIndexPath as a parameter:
方法 singleTap 和 doubleTap 只是以 NSIndexPath 作为参数无效:
- (void)singleTap:(NSIndexPath *)indexPath {
//do your stuff for a single tap
}
- (void)doubleTap:(NSIndexPath *)indexPath {
//do your stuff for a double tap
}
Hope it helps
希望能帮助到你
回答by limfinity
Solution for Swift 2:
Swift 2 的解决方案:
let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTapGesture.numberOfTapsRequired = 1 // Optional for single tap
view.addGestureRecognizer(singleTapGesture)
let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTapGesture.numberOfTapsRequired = 2
view.addGestureRecognizer(doubleTapGesture)
singleTapGesture.requireGestureRecognizerToFail(doubleTapGesture)
回答by iPhoneDeveloper
I implemented UIGestureRecognizerDelegate methods to detect both singleTap and doubleTap.
我实现了 UIGestureRecognizerDelegate 方法来检测 singleTap 和 doubleTap。
Just do this .
就这样做。
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleTapGesture:)];
[doubleTap setDelegate:self];
doubleTap.numberOfTapsRequired = 2;
[self.headerView addGestureRecognizer:doubleTap];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapGesture:)];
singleTap.numberOfTapsRequired = 1;
[singleTap setDelegate:self];
[doubleTap setDelaysTouchesBegan:YES];
[singleTap setDelaysTouchesBegan:YES];
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.headerView addGestureRecognizer:singleTap];
Then implement these delegate methods.
然后实现这些委托方法。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
回答by zerodog
Some view have there own double tap recognizers built in (MKMapView
being an example). To get around this you will need to implement UIGestureRecognizerDelegate
method shouldRecognizeSimultaneouslyWithGestureRecognizer
and return YES
:
有些视图内置了自己的双击识别器(MKMapView
例如)。要解决此问题,您需要实现UIGestureRecognizerDelegate
方法shouldRecognizeSimultaneouslyWithGestureRecognizer
并返回YES
:
First implement your double and single recognizers:
首先实现你的双识别器和单识别器:
// setup gesture recognizers
UITapGestureRecognizer* singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(mapViewTapped:)];
singleTapRecognizer.delegate = self;
singleTapRecognizer.numberOfTapsRequired = 1;
UITapGestureRecognizer* doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(mapViewDoubleTapped:)];
doubleTapRecognizer.delegate = self; // this allows
doubleTapRecognizer.numberOfTapsRequired = 2;
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
And then implement:
然后执行:
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer { return YES; }
回答by Oliver Dungey
In reference to @stanley's comment -
参考@stanley 的评论 -
Don't try and use tap gestures to row selections to work in UITableView
as it already has full tap handling....
不要尝试使用点击手势来行选择以进行工作,UITableView
因为它已经具有完整的点击处理....
But you must set 'Cancels Touches in View' to 'NO' on your single tap gesture recognizers or it will never get the tap events.
但是您必须在单击手势识别器上将“取消视图中的触摸”设置为“否”,否则它将永远不会获得点击事件。