Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Pan & LongPress Recognizers Simultanously

I have defined longPress and Pan gesture recognizers at viewController class level as below:

var touch = UILongPressGestureRecognizer()
var pan = UIPanGestureRecognizer()

Then I create a simple UIView:

let qBox = UIView()
qBox.frame = CGRect(x: 100, y: 200, width: 50, height: 50)
self.view.addSubview(qBox)

Then I configure and add my recognizers:

touch.addTarget(self, action: "ourTouched:")
touch.minimumPressDuration = 0
touch.numberOfTouchesRequired = 1
touch.numberOfTapsRequired = 0
qBox.addGestureRecognizer(touch)

pan.addTarget(self, action:"pan:")
pan.maximumNumberOfTouches = 1
pan.minimumNumberOfTouches = 1
self.view.addGestureRecognizer(pan)

Now, when I touch the qBox UIView, it triggers "ourTouched" method but if I keep holding and then start panning, it will not drag the qBox UIView. I have tried adding below line in my "ourTouched" function to remove the long press recognizer as soon as user touches the qBox UIView:

qBox.removeGestureRecognizer(touch)

But still, on first touch and drag, only long press method is called. I have to let go and then start again to pan. What am I missing?

like image 459
Kashif Avatar asked Feb 24 '15 14:02

Kashif


People also ask

How do you detect Pan gestures in Swift?

The UIPanGestureRecognizer class makes detecting pan gestures fairly straightforward. The heavy lifting is handled by UIPanGestureRecognizer and its superclass, UIGestureRecognizer. In this post, I show you how to use a pan gesture recognizer in Swift.

What is the difference between a pan and a swipe?

But somehow swipe is not being triggered (pan is triggered instead) Pan is recognized by the above sequence instead. If pan is commented, swipe is recognized by the same gesture .. With this, 2 questions: What is the difference then between a pan and a swipe? How can one simulate a swipe on iPhone simulator? Show activity on this post.

Why does my Pan recognize first but not the swipe?

Most likely, your pan recognizer "wins" the conflict because its gesture is simpler / more general: A swipe is a pan but a pan may not be a swipe, so the pan recognizes first and excludes other recognizers.

What is swift and how does it work?

SWIFT is a global member-owned cooperative and the world’s leading provider of secure financial messaging services.


1 Answers

Add a UIGestureRecognizerDelegate to your recognizers and have it return YES on shouldRecognizeSimultaneously.

{
  // setup ...
  touch.delegate = self
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
  return (gestureRecognizer == touch && otherGestureRecognizer == pan)
}

Update: Swift 4 or later

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return (gestureRecognizer == touch && otherGestureRecognizer == pan)
 }
like image 96
Ian MacDonald Avatar answered Sep 21 '22 06:09

Ian MacDonald