iphone - double tap fail safe way

2019-02-18 00:43发布

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 is always detected first.

How can I make this in a way that just detects the double tap?

I cannot use OS 3.x gestures, because I have to make it compatible with old OS versions.

thanks

3条回答
放我归山
2楼-- · 2019-02-18 00:52

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.

查看更多
倾城 Initia
3楼-- · 2019-02-18 00:54

Some excerpts from the tapZoom example of the scrollViewSuite sample code:

First, the function to kick off things once the touch ended:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 1) {

            [self performSelector: @selector(handleSingleTap)
                       withObject: nil
                       afterDelay: 0.35]; // after 0.35s we call it a single tap

    } else if([touch tapCount] == 2) {

            [self handleDoubleTap];
    }

}

Second: intercept the message if a new touch occurs during timeout:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [NSObject cancelPreviousPerformRequestsWithTarget: self
                                             selector: @selector(handleSingleTap)
                                               object: nil];
}

see also: http://developer.apple.com/iphone/library/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomingByTouch/ZoomingByTouch.html#//apple_ref/doc/uid/TP40008179-CH4-SW1

and here: (scrollView suite) http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/Introduction/Intro.html

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-18 00:59

Are you looking at the tapCount? For example:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
         UITouch *touch = [[event allTouches] anyObject];
         if (touch.tapCount == 2) {
                  //double-tap action here
         }
}
查看更多
登录 后发表回答