iOS touch events distinguish methods for double click responses

  • 2021-06-28 14:13:17
  • OfStack

If you have an view in your iPhone application, you have both a click and a double-click operation.When a user double-clicks view, the double-click operation is always performed after the one-click operation.So when you make a direct judgment, you will find that you cannot go directly into the double-click operation.Here's how to distinguish whether touch events are clicked or double-clicked


-(void)singleTap{
NSLog(@"Tap 1 time");
}
-(void)doubleTap{
NSLog(@"Tap 2 time");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSTimeInterval delaytime = 0.4;// Adjust to your needs 
switch (touch.tapCount) {
case 1:
[self performSelector:@selector(singleTap) withObject:nil afterDelay:delaytime];
break;
case 2:{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTap) object:nil];
[self performSelector:@selector(doubleTap) withObject:nil afterDelay:delaytime];
}
break;
default:
break;
}
}

The above is the iOS touch event distinguishing the double-click response method described by this site. I hope it will be helpful for you. If you have any questions, please leave a message for me. This site will reply to you in time!


Related articles: