Iphone - safe double tap method

I try to detect double taps in the view, but when a double tap comes in, the first click triggers an action on TouchesBegan, so before detecting a double tap, one tap will always be detected always.

How can I make it so that it simply detects a double click?

I cannot use OS 3.x gestures because I have to make it compatible with older versions of the OS.

thanks

+4
source share
3 answers

Some excerpts from the tapZoom example scrollViewSuite code example:

Firstly, the function to run things after the contact has 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]; } } 

Secondly: intercept the message if a new touch appears during the 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 package) http://developer.apple.com/iphone/library/samplecode/ScrollViewSuite/Introduction/Intro.html

+14
source share

Edit: I missed what you said you couldn't use 3.x gestures, so this is the wrong answer to your question, but I leave it in case someone who can use 3.x gestures can benefit from this.

You can create two gesture recognizers: one for one 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 now, here comes the blow:

 [singleTapGesture requireGestureRecognizerToFail : doubleTapGesture]; 

The last line makes your single descriptor a handler only if the double tap fails. This way you get both a single tap and a double tap in your application.

+6
source share

Are you looking at tapCount? For example:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; if (touch.tapCount == 2) { //double-tap action here } } 
+2
source share

All Articles