如何在3.2以下的iOS中获得长按手势的功能

发布于 2024-10-09 23:04:52 字数 122 浏览 6 评论 0原文

UILongPressGesture 在 ios 版本 3.2 及更高版本中可用。但我正在尝试开发应用程序以实现最大兼容性,因此针对 ios ver2.0

任何人都可以指导我如何在 ios v2.0 中完成长按手势

UILongPressGesture is available in ios ver 3.2 and later. But i am trying to develop application for maximum compatibility and hence targeting ios ver2.0

Can anyone please guide me on how to accomplish long press gesture in ios v2.0

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

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

发布评论

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

评论(2

瑕疵 2024-10-16 23:04:52

对于单个手指,这非常简单:在 touchesBegan 方法中启动一个计时器,并在计时器触发时触发一个操作。如果在触发之前收到 touchesEnded 消息,请取消计时器。下面是使用 performSelector:withObject:afterDelay: 方法的实现。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(fireLongPress)
               withObject:nil
               afterDelay:LONG_PRESS_THRESHOLD];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

- (void)fireLongPress {
    // do what you want to do
}

如果手指移动得太远,您可能还想终止计时器。

对于多点触控,情况会稍微复杂一些。您必须跟踪哪个触摸是哪个触摸,并决定要做什么,例如,当一个手指按下足够长的时间而另一根手指没有按下时(或者弄清楚UILongPressGestureRecognizer做了什么)。

For a single finger, it's pretty simple: Start a timer in the touchesBegan method and trigger an action when the timer fires. Cancel the timer if you get a touchesEnded before it fires. Here's an implementation that uses the performSelector:withObject:afterDelay: method.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(fireLongPress)
               withObject:nil
               afterDelay:LONG_PRESS_THRESHOLD];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

- (void)fireLongPress {
    // do what you want to do
}

You'll probably also want to kill the timer if the finger moves too far.

With multitouch, it's a bit more complicated. You'll have to keep track of which touch is which, and decide what to do e.g. when one finger has pressed long enough but the other hasn't (or figure out what UILongPressGestureRecognizer does).

白馒头 2024-10-16 23:04:52

在您的视图中实现 touches... 方法。如果在 touchesBegan:withEvent:touchesEnded:withEvent: 之间经过一定时间而没有任何 touchesMoved:withEvent: 事件,则您有一个很长的时间按。

Implement the touches... methods in your view. If a certain amount of time passes between touchesBegan:withEvent: and touchesEnded:withEvent: without any touchesMoved:withEvent: events, you have a long press.

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