7

I add singTap and doubleTap to a view like the code below:

-(void)awakeFromNib{
    [self setUserInteractionEnabled:YES];
    //
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];
    doubleTapGesture.numberOfTapsRequired = 2;
    [self addGestureRecognizer:doubleTapGesture];

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
    singleTapGesture.numberOfTapsRequired = 1;
    [self addGestureRecognizer:singleTapGesture];
}

-(void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTapGesture{
    [[self delegate] singleTapOnView];
}

-(void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTapGesture{
    [[self delegate] doubleTapOnView];
}

When doubleTap the singleTap also fire. How to disable singleTap when doubleTap? Thanks!

Jack
  • 81
  • 8

1 Answers1

11

Tell the single-tap recognizer that it requires the double-tap recognizer to fail before it triggers:

[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];

(This is actually the canonical example in the UIGestureRecognizer docs for this method.)

Tim
  • 59,527
  • 19
  • 156
  • 165