Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement singleTap and doubleTap in Objective-C

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!

like image 894
Jack Avatar asked Mar 22 '13 01:03

Jack


1 Answers

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.)

like image 156
Tim Avatar answered Sep 20 '22 10:09

Tim