pneves
pneves

Reputation: 13

Double tap in UIGestureRecognizer

My project has a custom uitableviewCell which has a image slider inside. basically a scrollview with images paginated.

I need to tap and double tap this ImageSlider to perform some actions. The single tap is working fine, but the double tap selector is not triggered.

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")
}

regards

EDIT : you can see my Log here, i changed my original post too

 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

Upvotes: 1

Views: 1008

Answers (1)

danh
danh

Reputation: 62686

To make a single and double tap gesture work together, the system, upon seeing a single, needs to know if it's really a single or the first half of a double. It must either have knowledge of the future (which would be a very valuable feature) or it must wait and see what comes next. To tell it to wait and see...

singleTap.requireGestureRecognizerToFail(doubleTap)

Upvotes: 5

Related Questions