有人有替代 UILongPressGestureRecognizer 的代码吗?

发布于 2024-10-10 18:19:47 字数 119 浏览 3 评论 0原文

我想支持 3.2 之前的版本,这是唯一不想合作的符号,有人知道一些 Touchmove 代码或我可以用来代替 UILongPressGestureRecognizer 的东西吗?

谢谢,

尼克

I'd like to support pre 3.2 and this is the only symbol that doesn't want to cooperate, anyone know of some touchesmoved code or something i can use in lieu of the UILongPressGestureRecognizer?

Thanks,

Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我入戏太深 2024-10-17 18:19:48

如您所知,对于 3.2 之前的 iOS,您应该使用 TouchBegan、Moved、Ended 和 Canceled 函数。
我认为只实现touchesMoved是不好的,因为如果用户按下并且在释放之前根本不移动,那么touchesMoved将永远不会被调用。

相反,我使用 NSTimer 来实现长按触摸事件。
这可能不是最佳解决方案,但它对我的应用程序运行良好。
这是一段代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    isAvailable = NO;
    timer = [NSTimer scheduledTimerWithTimeInterval:DURATION target:self selector:@selector(didPassTime:) userInfo:nil repeats:NO];
}

- (void)didPassTime:(id)sender{
    isAvailable = YES;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if(isAvailable == YES){
        // still pressing after 0.5 seconds 
    }
    else{
        // still pressing before 0.5 seconds
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if(isAvailable == YES){
        // releasing a finger after 0.5 seconds
    }
    else {
        // releasing a finger before 0.5 seconds
            [timer invalidate];
            timer = nil;
    }



}

As you know, you should use touchesBegan, Moved, Ended, and Canceled functions for pre 3.2 iOS.
I think implementing only touchesMoved is bad because if the user presses and doesn't move at all until releasing, then touchesMoved won't get called ever.

Instead, I used NSTimer to acheive a long press touch event.
This might not be an optimal solution, but it worked well for my app.
Here's a snippet of code.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    isAvailable = NO;
    timer = [NSTimer scheduledTimerWithTimeInterval:DURATION target:self selector:@selector(didPassTime:) userInfo:nil repeats:NO];
}

- (void)didPassTime:(id)sender{
    isAvailable = YES;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    if(isAvailable == YES){
        // still pressing after 0.5 seconds 
    }
    else{
        // still pressing before 0.5 seconds
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if(isAvailable == YES){
        // releasing a finger after 0.5 seconds
    }
    else {
        // releasing a finger before 0.5 seconds
            [timer invalidate];
            timer = nil;
    }



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文