在UIScrollView中同时允许单击手势识别器和双击

所以我在UIScrollView里面有一个UIImageView,我需要捕获它们: – 单击手势 – UIImageView的双击手势

双击手势应该作为缩放动作发送到UIScrollView。单击手势由我自己创建的UITapGestureRecognizer捕获。 双击手势具有比单击更高的优先级(当有双击时,放大滚动视图,否则执行单击)

到目前为止,如果我将单击手势识别器添加到滚动视图,则立即识别单击并且不能识别双击。

如果我将单击手势识别器添加到imageview,它从不接收任何操作,但双击工作

任何建议表示赞赏,谢谢..

试试这段代码,

目标 – C.

UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; singletap.numberOfTapsRequired = 1; singletap.numberOfTouchesRequired = 1; [scrollView addGestureRecognizer:singletap]; UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)]; doubleTap.numberOfTapsRequired =2 ; doubleTap.numberOfTouchesRequired = 1; [scrollView addGestureRecognizer:doubleTap]; [singleTap requireGestureRecognizerToFail:doubleTap]; 

Swift 3.0

 var singletap = UITapGestureRecognizer(target: self, action: #selector(self.singleTap)) singletap.numberOfTapsRequired = 1 singletap.numberOfTouchesRequired = 1 scrollView.addGestureRecognizer(singletap) var doubleTap = UITapGestureRecognizer(target: self, action: #selector(self.doubleTap)) doubleTap.numberOfTapsRequired = 2 doubleTap.numberOfTouchesRequired = 1 scrollView.addGestureRecognizer(doubleTap) singleTap.require(toFail: doubleTap) 

最佳答案实际上是最后一行倒置。 正确的代码应该是:

 [singleTap requireGestureRecognizerToFail:doubleTap]; 
 UITapGestureRecognizer *tapRecognizerForSingleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)]; [tapRecognizerForSingleTap setNumberOfTapsRequired:1]; UITapGestureRecognizer *tapRecognizerForDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)]; [tapRecognizerForDoubleTap setNumberOfTapsRequired:2]; 

两者都添加在同一图像视图中,并实现您想要的只需添加以下行。

 [tapRecognizerForSingleTap requireGestureRecognizerToFail:tapRecognizerForDoubleTap]; 

通过以上行,您要求单击才能识别它是否不是双击。