With a similar problem to this question, I am trying to add a double tap gesture recognizer to my UICollectionView
instance.
I need to prevent the default single tap from calling the UICollectionViewDelegate
method collectionView:didSelectItemAtIndexPath:
.
In order to achieve this I implement the code straight from Apple's Collection View Programming Guide (Listing 4-2):
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; NSArray* recognizers = [self.collectionView gestureRecognizers]; // Make the default gesture recognizer wait until the custom one fails. for (UIGestureRecognizer* aRecognizer in recognizers) { if ([aRecognizer isKindOfClass:[UITapGestureRecognizer class]]) [aRecognizer requireGestureRecognizerToFail:tapGesture]; } // Now add the gesture recognizer to the collection view. tapGesture.numberOfTapsRequired = 2; [self.collectionView addGestureRecognizer:tapGesture];
This code does not work as expected: tapGesture
fires on a double tap but the default single tap is not prevented and the delegate's didSelect...
method is still called.
Stepping through in the debugger reveals that the if condition, [aRecognizer isKindOfClass:[UITapGestureRecognizer class]]
, never evaluates to true and so the failure-requirement on the new tapGesture
is not being established.
Running this debugger command each time through the for-loop:
po (void)NSLog(@"%@",(NSString *)NSStringFromClass([aRecognizer class]))
reveals that the default gesture recognizers are (indeed) not UITapGestureRecognizer
instances.
Instead they are private classes UIScrollViewDelayedTouchesBeganGestureRecognizer
and UIScrollViewPanGestureRecognizer
.
First, I can't use these explicitly without breaking the rules about Private API. Second, attaching to the UIScrollViewDelayedTouchesBeganGestureRecognizer
via requireGestureRecognizerToFail:
doesn't appear to provide the desired behaviour anyway — i.e. the delegate's didSelect...
is still called.
How can I work with UICollectionView
's default gesture recognizers to add a double tap to the collection view and prevent the default single tap from also firing the delegate's collectionView:didSelectItemAtIndexPath:
method?
Thanks in advance!