我正在尝试向视图添加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
{
}
答案 0 :(得分:388)
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];
注意: 如果您使用的是 numberOfTouchesRequired
,则必须 {{1} }
对于Swift
.numberOfTouchesRequired = 1;
答案 1 :(得分:39)
Swift 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)
在代码行 singleTap.require(toFail:doubleTap) 中,我们强制单击等待并确保点击事件不是双击。
答案 2 :(得分:31)
您需要使用requireGestureRecognizerToFail:方法。像这样:
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
答案 3 :(得分:15)
// ----首先你必须分配双击和单击手势------- //
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];
// -----------------------点击次数---------------- // < / p>
[doubleTap setNumberOfTapsRequired : 2];
[singleTap setNumberOfTapsRequired : 1];
// -------在视图或按钮上添加双击和单击手势-------- //
[self.view addGestureRecognizer : doubleTap];
[self.view addGestureRecognizer : singleTap];
答案 4 :(得分:10)
不确定这究竟是你在寻找什么,但我没有手势识别器就做了单/双击。我在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;
使用NSIndexPath作为参数,方法singleTap和doubleTap只是空的:
- (void)singleTap:(NSIndexPath *)indexPath {
//do your stuff for a single tap
}
- (void)doubleTap:(NSIndexPath *)indexPath {
//do your stuff for a double tap
}
希望有所帮助
答案 5 :(得分:3)
Swift 2的解决方案:
.ebextensions/efs.config
答案 6 :(得分:1)
我实现了UIGestureRecognizerDelegate方法来检测singleTap和doubleTap。
就这样做。
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];
然后实现这些委托方法。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
答案 7 :(得分:0)
某些视图内置了自己的双击识别器(MKMapView
为例)。要解决此问题,您需要实施UIGestureRecognizerDelegate
方法shouldRecognizeSimultaneouslyWithGestureRecognizer
并返回YES
:
首先实现双重和单一识别器:
// 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];
然后实施:
#pragma mark UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer { return YES; }
答案 8 :(得分:-1)
参考@ stanley的评论 -
请勿尝试使用点击手势将选项排成UITableView
,因为它已经完全点击处理....
但是你必须在视图中设置&#39;取消触摸&#39;到了没有&#39;在您的单击手势识别器上,或者它永远不会获得点击事件。