UITapGestureRecognizer – 单击和双击

我正在尝试将2个UITapGestureRecognizers添加到视图中,一个用于单击,另一个用于双击事件。 单击识别器按预期工作(单独)。 但是我似乎无法获得双击识别器的工作。

试图尝试类似的属性: cancelsTouchesInViewdelaysTouchesBegandelaysTouchesEnded但仍然无法正常工作。

当我双击时,单击的识别器总是被激活,双击事件也会被发送到超级视图。 但是自定义双击识别器似乎没有被通知。

文件似乎表明,上面提到的三个属性可以用于这个目的。 但是我只是不确定应该设置哪些值以及哪些识别器(单个,双重或两者)。 希望有人熟悉这可以帮助。

以下是最新更新的代码块。

 // ****** 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 { } 
 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 它必须是 .numberOfTouchesRequired = 1;

您需要使用requireGestureRecognizerToFail:方法。 像这样的东西:

[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];

Swift 3解决scheme:

 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)中,我们迫使单击等待,并确保轻事件不是双击。

// —-首先你必须分配双击和单击手势——- //

 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]; 

// ———————–点击次数—————- //

 [doubleTap setNumberOfTapsRequired : 2]; [singleTap setNumberOfTapsRequired : 1]; 

// ——-在视图或button上添加双击和单击手势——– //

 [self.view addGestureRecognizer : doubleTap]; [self.view addGestureRecognizer : singleTap]; 

不知道是否你正在寻找什么,但我没有手势识别器单击/双击。 我在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; 

方法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 } 

希望它有帮助

Swift 2的解决scheme:

 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) 

有些视图有自己的双击识别器( 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; } 

我实现了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; } 

在参考@斯坦利的评论 –

不要尝试使用轻敲手势来排列select在UITableView工作,因为它已经有了完整的水龙头处理….

但是,您必须在您的单击手势识别器上将“取消触摸视图”设置为“否”,否则将永远不会获得轻敲事件。