开发者

How to prevent simultaneous UIGestureRecognizers

开发者 https://www.devze.com 2023-04-03 03:09 出处:网络
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, so

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:

  • gestur开发者_如何学JAVAeRecognizer: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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消