简体   繁体   中英

Double tap in cells doesn't work on iOS9

Our project has custom UICollectionViewCell that handles single tap and double tap by using UITapGestureRecognizers, and a subview that also uses a single tap UITapGestureRecognizer.

This is how the single/double tap set up in our project:

    _singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    _singleTapGestureRecognizer.numberOfTapsRequired = 1;
    [self addGestureRecognizer:_singleTapGestureRecognizer];

    _doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    _doubleTapGestureRecognizer.numberOfTapsRequired = 2;
    [self addGestureRecognizer:_doubleTapGestureRecognizer]

    [_singleTapGestureRecognizer requireGestureRecognizerToFail:_doubleTapGestureRecognizer];

In our code, we're also canceling single/double taps by doing these,

    self.singleTapGestureRecognizer.enabled = NO;

    self.doubleTapGestureRecognizer.enabled = NO;
    self.doubleTapGestureRecognizer.enabled = YES;

    self.singleTapGestureRecognizer.enabled = YES;

These used to work great on pre-iOS9 devices, recently, however, I found that on iOS9 devices, the double tap is never triggered. Every time I tried to double tap on the cell, it immediately registers a single tap with almost no delay.

When I tried to look into it, I found that when singleTapGestureRecognizer is registered, the doubleTapGestureRecognizer is still in state Possible not Failed.

single tap: <UITapGestureRecognizer: 0x7f94ead42220; state = Ended; view = <UIView 0x7f94ead3f710>; target= <(action=handleSingleTap:, target=<PVEntityStreamCellForSetOnboarding 0x7f94ead3f1e0>)>; must-fail = {
<UITapGestureRecognizer: 0x7f94ead42810; state = Possible; view = <UIView 0x7f94ead3f710>; target= <(action=handleDoubleTap:, target=<PVEntityStreamCellForSetOnboarding 0x7f94ead3f1e0>)>; numberOfTapsRequired = 2>
}>

I also found that if I tap on the subview which contains another UITapGestureRecognizer first, then double tap will work, once I scroll the collection view, it's impossible to do double tap on any cells.

Does anyone has similar issues or know how to fix this?

I had a similar issue and in my case the fix was implementing a delegate method for singleTapGestureRecognizer. I also tried to use canceling for single/double taps, but without success. Now, my code looks like this (it is written in swift, but it's easy to read):

let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleSingleTap:"))
let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleDoubleTap:"))

singleTapGestureRecognizer.numberOfTapsRequired = 1
doubleTapGestureRecognizer.numberOfTapsRequired = 2

singleTapGestureRecognizer.delegate = self

singleTapGestureRecognizer.requireGestureRecognizerToFail(doubleTapGestureRecognizer)
view.addGestureRecognizer(singleTapGestureRecognizer)
view.addGestureRecognizer(doubleTapGestureRecognizer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM