iphone - double tap fail safe way

后端 未结 3 1046
后悔当初
后悔当初 2021-01-13 18:44

I am trying to detect double taps on a view, but when the double tap comes, the first tap triggers an action on TouchesBegan, so, before detecting a double tap a single tap

3条回答
  •  Happy的楠姐
    2021-01-13 19:08

    Edit: I missed the point that you said you cannot use 3.x gestures, so this is an invalid answer to your question, but I'm leaving it in case someone who can use 3.x gestures may benefit from it.

    You can create two gesture recognizers, one for single tap and one for double tap:

    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)];
    singleTapGesture.cancelsTouchesInView = NO; 
    singleTapGesture.delaysTouchesEnded = NO;
    singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap
    singleTapGesture.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:singleTapGesture];
    [singleTapGesture release];
    
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)];
    doubleTapGesture.cancelsTouchesInView = NO; 
    doubleTapGesture.delaysTouchesEnded = NO;
    doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap
    doubleTapGesture.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTapGesture];
    [doubleTapGesture release];
    

    And then, here comes the punch:

    [singleTapGesture requireGestureRecognizerToFail : doubleTapGesture];
    

    The last line, makes your single tap handler work only if the double tap fails. So, you get both single tap and double tap in your application.

提交回复
热议问题