如何在iPhone屏幕上跟踪触摸事件2秒钟。就像在Safari中保存图像一样,添加到UIWebView中的图像?
答案 0 :(得分:10)
在视图的+scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
方法中使用-touchesBegan:withEvent:
创建一个NSTimer,并在-invalidate
中取消它(使用-touchesEnded:withEvent:
)。如果其选择器指向的方法被调用,则用户将其手指放在视图上,无论您将计时器的间隔设置为何时。例如:
接口(.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
}