iphone - 如何在 iPhone 中追踪手指的长按操作?

标签 iphone events touch

我如何跟踪 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/

相关文章:

iphone - 我无法使用这个简单的 LLDB 别名

ios - 借助 iPhone 应用程序的 Web 服务发送数据是否有任何限制?

jquery - 当在类上使用 jQuery 的单击事件时,如何避免为所有具有该类名的事件调用该事件?

javascript - Hammer.js (IE8)-对象不支持属性或方法 'addEventListener'

ios - Swift:与相同颜色的 UIView 相比,UINavigationBar 看起来很轻

iphone - 如何提交适用于iPhone的免费应用程序和适用于iPad的付费应用程序?

javascript - angular $broadcast 和 $on 贵吗?

jquery - 为什么替换 html 时的 Backbone 事件不起作用?

android - 在 Unity 中围绕 X 和 Y 轴旋转对象问题

android - 如何以编程方式在 Android 中启用 "show touches"选项?