iphone - double tap fail safe way

青春壹個敷衍的年華 提交于 2019-12-01 05:33:51

问题


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


回答1:


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




回答2:


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.




回答3:


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
         }
}


来源:https://stackoverflow.com/questions/2637229/iphone-double-tap-fail-safe-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!