Search code examples
swiftuipangesturerecognizer

Only one PanGesture can be called a time


For example, I have multiple buttons with multiple pangestures like this

   let buttonGes: UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(buttonPanGesture(recognizer:)))
   button.addGestureRecognizer(buttonGes)

My problem is that while I'm dragging a button and try to drag another one at the same time, my logic will not working properly. Therefore, I want to limit only 1 pangesture can be called at a time, how do I do that?

Update: I tried a method that at state .began, I make other buttons isUserInteractionEnabled = false and re-enable at state .ended. It worked fine but I don't know if it would be the most optimized code or there is better methods.


Solution

  • for this job, you should use UIGestureRecognizerDelegate for better clarity