views:

575

answers:

2

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

A: 

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
         }
}
David Gelhar
+4  A: 

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

Efrain
thanks. Sorry for the delay.
Digital Robot