English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Methods to Differentiate Click and Double-click Responses of Touch Events in iOS

如果您的 iPhone 应用里有个 view,既有单击操作又有双击操作。用户双击 view 时,总是先执行一遍单击的操作再执行双击的操作。所以直接判断时就会发现不能直接进入双击操作。下面是区分 touch 事件是单击还是双击的方法

-(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 according 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-mentioned method of distinguishing between single-click and double-click responses for iOS touch events introduced by the editor to everyone is hoped to be helpful to everyone. If you have any questions, please leave me a message, and the editor will reply to everyone in time!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any infringement, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like