如何防止同时出现 UIGestureRecognizers

发布于 2024-12-03 10:57:35 字数 474 浏览 0 评论 0原文

我在几个类似卡片的视图上使用 UIPanGestureRecognizer 来让用户在屏幕上移动视图。他们可以同时放下 3 个手指并拾取 3 张卡片,这真是太好了,但是,我的某些功能并不是这样设计的。

我想一次只允许 1 个手势识别器运行。有没有更好的方法来做到这一点?

我考虑过:

  • gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 但默认情况下它已经返回“NO”。
  • 在第一个手势开始时设置一个实例变量,但我担心对该变量的多线程访问(我应该使用@synchronized,还是会开销太大?)。
  • 保留手势识别器数组并检查它们在gestureRecognizerShouldBegin: 中的状态,以确保没有任何手势识别器正在进行中。

谢谢。

I am using a UIPanGestureRecognizer on several card-like views to let the user move the views around the screen. It's very nice that they can put down 3 fingers and pickup 3 cards at once, however, some of my functionality isn't designed to work like that.

I'd like to only allow 1 gesture recognizer to run at a time. Is there a preferred way to do this?

I've considered:

  • gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: but it already returns 'NO' by default.
  • Setting an instance variable when the first gesture begins, but I'm concerned about multithreaded access to this variable (Should I use @synchronized, or would it be too much overhead?).
  • Keeping an array of the gesture recognizers and checking their state in gestureRecognizerShouldBegin: to ensure that none are in progress.

Thanks.

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

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

发布评论

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

评论(2

2024-12-10 10:57:35

最佳实践是在视图中使用一个(全局)手势识别器,该识别器是您的卡片的超级视图,并使用 hitTest: 来确定哪张卡片已被触摸。它将允许您正确地处理多个触摸。

The best practice is using one (global) gesture recognizer in view that's being superview for your cards with hitTest: for determining which card has been touched. It will allow you to work with multiple touches correctly.

日暮斜阳 2024-12-10 10:57:35

将单个 UIPanGestureRecognizer 放在所有卡片的公共超级视图上,然后进行命中检测以在手势开始时找到有问题的卡片。这样您就只有 1 个手势识别器,因此一次只能运行一个手势。

编辑:顺便说一句,你保留伊瓦尔的想法虽然笨拙,但会奏效。 UIGestureRecognizer 是 UIKit 的一部分,仅在主线程上操作,因此您不必担心多线程访问。但就像我说的,这很笨拙。使用单个“主”UIGestureRecognizer 反而更干净。

Put a single UIPanGestureRecognizer on the common superview of all your cards, and then do hit detection to find the card in question when the gesture starts. That way you only have 1 gesture recognizer, so only one gesture can run at a time.

Edit: BTW, your idea of keeping an ivar, while clumsy, would work. UIGestureRecognizer is part of UIKit and only operates on the main thread, so you don't have to worry about multithreaded access. But like I said, it's clumsy. Using a single "master" UIGestureRecognizer instead is cleaner.

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