4

我怎么能在 iPhone 屏幕上跟踪触摸 2 秒之类的事件。就像在 Safari 中为添加到 UIWebView 的图像保存图像一样?

4

1 回答 1

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
}
于 2010-02-07T03:31:22.483 回答