SpaceDog SpaceDog - 4 months ago 29
iOS Question

iPhone - gesture recognizers fighting each other

I have added two gesture recognizers to a view. One will handle the view drag and the other the double tap. Something like

UITapGestureRecognizer *doubleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
[doubleTap setDelegate:self];
[doubleTap setCancelsTouchesInView:YES];
[doubleTap setNumberOfTapsRequired:1];
[base addGestureRecognizer:doubleTap];
[doubleTap release];

UIPanGestureRecognizer *panGesture = nil;
panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(drag:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setDelegate:self];
[base addGestureRecognizer:panGesture];
[panGesture release];

The problem is this: as the view can move, the double tap is somehow difficult to obtain, because the view can slide one hair to one side or the other and iOS will not recognize it as double tap, and instead will see it as two moves and the drag method will run twice.

I am not seeing how this can be done. Any clues?



There's a delegate method that will tell the central gesture code that two recognizers might be going at the same time:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    if (gestureRecognizer==_panRecognizer && otherGestureRecognizer==_swipeRecognizer)
        return YES;
    if (gestureRecognizer==_swipeRecognizer && otherGestureRecognizer==_panRecognizer)
        return YES;
    return NO;

Maybe you can try something like that.