Detecting only double or single tap with UIViews?

I want to detect JUST double / single tap when the user touches the view.

I did something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint prevLoc = [touch ] if(touch.tapCount == 2) NSLog(@"tapCount 2"); else if(touch.tapCount == 1) NSLog(@"tapCount 1"); } 

But he always detects 1 touch to 2 taps. How can I detect only 1/2 tap?

+4
source share
3 answers

Thanks for the help. I also found a way:

 -(void)handleSingleTap { NSLog(@"tapCount 1"); } -(void)handleDoubleTap { NSLog(@"tapCount 2"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSUInteger numTaps = [[touches anyObject] tapCount]; float delay = 0.2; if (numTaps < 2) { [self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:delay ]; [self.nextResponder touchesEnded:touches withEvent:event]; } else if(numTaps == 2) { [NSObject cancelPreviousPerformRequestsWithTarget:self]; [self performSelector:@selector(handleDoubleTap) withObject:nil afterDelay:delay ]; } } 
+3
source

This will help identify methods for single and double bends.

 (void) handleSingleTap {} (void) handleDoubleTap {} 

So then in touchesEnded you can call the appropriate method based on the number of taps, but only after the delay has gone beyond the handleSingleTap call to make sure that the double tap has not been executed:

 -(void) touchesEnded(NSSet *)touches withEvent:(UIEvent *)event { if ([touch tapCount] == 1) { [self performSelector:@selector(handleSingleTap) withObject:nil afterDelay:0.3]; //delay of 0.3 seconds } else if([touch tapCount] == 2) { [self handleDoubleTap]; } } 

In touchesBegan cancel all requests for handleSingleTap so that the second answer cancels the first call to handleSingleTap and only handleDoubleTap will be called

 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(handleSingleTap) object:nil]; 
+2
source

Maby you can use a certain time interval. Wait by sending an event for (x) ms. If you get two taps in this time period, send a double tap. If you get only a single click of a button.

0
source

Source: https://habr.com/ru/post/1341626/


All Articles