双击UIGestureRecognizer

时间:2017-01-19 16:41:31

标签: ios objective-c iphone swift

我的项目有一个自定义的uitableviewCell,里面有一个图像滑块。基本上是一个图像分页的滚动视图。

我需要点按并双击此ImageSlider以执行某些操作。单击可以正常工作,但不会触发双击选择器。

override func awakeFromNib() {
    super.awakeFromNib()

    let singleTap = UITapGestureRecognizer(target: self, action: #selector(singleTap(_:)))
    singleTap.numberOfTapsRequired = 1
    singleTap.cancelsTouchesInView = false
    slideShowView.addGestureRecognizer(singleTap)


    //Double tap recognizer
    let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTap(_:)))
    doubleTap.cancelsTouchesInView = false
    doubleTap.numberOfTapsRequired = 2

    slideShowView.addGestureRecognizer(doubleTap)

    singleTap.require(toFail: doubleTap)

    print("slideshow recognizers",slideShowView.gestureRecognizers!) }         


func doubleTap(_ sender : UITapGestureRecognizer) {

     print("doubletap")        
}

 func singleTap(_ sender : UITapGestureRecognizer) {

     print("singleTap")
}

问候

编辑:你可以在这里看到我的日志,我也改变了原来的帖子

 slideshow recognizers [<UITapGestureRecognizer: 0x17ec1670; state = Possible; view = <ImageSlideshow.ImageSlideshow 0x17ec0e30>; target= <(action=singleTap:, target=<Descubra.FeedDefaultCell 0x183cf800>)>; must-fail = {
    <UITapGestureRecognizer: 0x17e763b0; state = Possible; view = <ImageSlideshow.ImageSlideshow 0x17ec0e30>; target= <(action=doubleTap:, target=<Descubra.FeedDefaultCell 0x183cf800>)>; numberOfTapsRequired = 2>
}>, <UITapGestureRecognizer: 0x17e763b0; state = Possible; view = <ImageSlideshow.ImageSlideshow 0x17ec0e30>; target= <(action=doubleTap:, target=<Descubra.FeedDefaultCell 0x183cf800>)>; numberOfTapsRequired = 2; must-fail-for = {
    <UITapGestureRecognizer: 0x17ec1670; state = Possible; view = <ImageSlideshow.ImageSlideshow 0x17ec0e30>; target= <(action=singleTap:, target=<Descubra.FeedDefaultCell 0x183cf800>)>>
}>]

singleTap

1 个答案:

答案 0 :(得分:5)

为了使单击和双击手势协同工作,系统在看到单个时需要知道它是真的单个还是双倍的前半部分。它必须要么了解未来(这将是一个非常有价值的特征),要么必须等待,看看接下来会发生什么。告诉它等等......

singleTap.requireGestureRecognizerToFail(doubleTap)