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

时间:2015-03-27 10:32:14

标签: ios objective-c uiscrollview

所以我在UIScrollView中有一个UIImageView,我需要捕获两者: - 单击手势 - 双击手势 对于UIImageView

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

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

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

感谢任何建议,谢谢..

3 个答案:

答案 0 :(得分:6)

试试这段代码,

目标 - 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)

答案 1 :(得分:2)

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

{{1}}

答案 2 :(得分:0)

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

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