iphone - iOS 手势问题 - 如何检测持续触摸特定时间间隔的触摸事件?

标签 iphone ios ipad gesture

我对 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;
}

关于iphone - iOS 手势问题 - 如何检测持续触摸特定时间间隔的触摸事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007364/

相关文章:

iphone - 防止 iOS 移动 safari 闲置/自动锁定/休眠?

ios - Google Maps iOS 禁用旋转和倾斜

ios - 将 UIViewController 添加到 UIScrollView swift 3

javascript - 完美的触摸就绪、iPad 兼容和内容滑动就绪网站的框架

ios - iPad 多任务支持需要这些方向

iphone - ios 电话间隙数字键盘

iphone - 如何从 UITableView 的多个部分获取选定行的一部分?

iphone - 理解 Objective c 枚举声明

iphone - 使用 UIImagePickerController 显示相同的默认相机应用程序

javascript - iOS 7 中横幅/网页右侧不需要的空间条