我在iOS的触摸事件和手势很新。的iOS手势的问题 - 如何检测其保持触摸一定时间间隔的触摸事件?
我需要当用户保持他的手指触摸像3秒一定的时间间隔触发事件。如何监控这种长时间的事件?
感激,如果你能给我一些代码,以供参考?
谢谢。
我在iOS的触摸事件和手势很新。的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;
}
你可能想看看如果UILongPressGestureRecognizer适合您的应用程序接口。上面的苹果文档有一个示例代码链接。 UIView只提供了四种重写方法。您可以使用手势识别器进行更多操作。 Here's其他手势识别器的示例代码。希望这可以帮助。