我对 iOS 中的触摸事件和手势很陌生。
我需要在用户保持手指触摸特定时间间隔(如 3 秒)时触发一个事件。如何监控这种长按事件?
能否提供一些代码供我引用,不胜感激?
谢谢。
最佳答案
你可以这样做。
在你的 .h 文件中:
NSTimer *touchesHoldTimer;
和:
@property (nonatomic, retain) NSTimer *touchesHoldTimer;
- (void)touchesHoldCheckTime;
记得合成释放touchesHoldTimer
在您的 .m 文件中:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *countTouches = [event allTouches];
NSLog(@"touchesBegan");
if ([countTouches count] == 1) { // Not multitouch
NSLog(@"Starting timer..");
touchesHoldTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(touchesHoldCheckTime) userInfo:nil repeats:NO];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesEnded");
if (touchesHoldTimer != nil) {
[touchesHoldTimer invalidate];
touchesHoldTimer = nil;
}
}
- (void)touchesHoldCheckTime {
NSLog(@"You have hold me down for 3 sec.");
[touchesHoldTimer invalidate];
touchesHoldTimer = nil;
}
关于iphone - iOS 手势问题 - 如何检测持续触摸特定时间间隔的触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007364/