我如何跟踪 iPhone 屏幕上触摸 2 秒之类的事件。就像在 Safari 中保存添加到 UIWebView 的图像一样?
最佳答案
在 View 的 -touchesBegan:withEvent:
方法中使用 +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
创建一个 NSTimer,并取消它(使用 -invalidate
) 在 -touchesEnded:withEvent:
中。如果其选择器指向的方法被调用,则用户将手指放在 View 上,无论您设置计时器的时间间隔为多少时间。示例:
接口(interface)(.h):
@interface MyView : UIView
...
NSTimer *holdTimer;
@end
实现(.m):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = nil;
}
- (void)touchWasHeld
{
holdTimer = nil;
// do your "held" behavior here
}
关于iphone - 如何在 iPhone 中追踪手指的长按操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2215784/