Executing pan gesture recognizer ONLY after long press recognizer has fired

1.5k views Asked by At

I'm trying to implement a drag and drop UI for my UIView using the pan gesture recognizer. I have that piece of code working, but now I want to only execute the drag and drop logic only AFTER the user has long pressed on my to-be-dragged view.

I'm implementing the code in the below question Recognize long press and pan gesture recognizers together but it's not exactly what I want. Any idea?

2

There are 2 answers

2
Duncan C On

Set up your view controller as the delegate of the pan gesture recognizer.

Implement the gestureRecognizerShouldBegin(_:) method. Return false until after the long press gesture recognizer fires.

0
7ball On

Found another post whose title was a bit misleading so I didn't look into it too much the first time. Combine longpress gesture and drag gesture together

It turns out, UILongPressGesture already can help me achieve the drag and drop effect that I want. That means I do NOT need the UIPanGesture at all. I just used selector/handler for the pan gesture for the long press gesture. Except the long press gesture doesn't have the translation properties, so I use

myView.center = sender.location(in: myView.superview)

to achieve the same dragging effect.