0

I want to implement a pan gesture ONLY after a long press has been detected. I'm monitoring the longpress gesture for "UIGestureRecognizerState.Changed", and calling my selector for handling the panning there. The selector fires (my "print()" check is displayed in the console), but I have to lift my finger before the uiview (blueRec) will actually move/translate.

I understand both the long press and pan gestures are continuos in nature, but how can I use the initial press that fired the longpress to ALSO pan the touched uiview (blueRec)? I don't believe this is at all a case for - requireGestureRecognizerToFail:

I've stripped down the code below to just contain essential lines.

@IBOutlet weak var graySuper: UIView!
@IBOutlet weak var blueRec: UIView!

@IBOutlet weak var blueLeading: NSLayoutConstraint!
@IBOutlet weak var blueTop: NSLayoutConstraint!

var longPressGesture:UILongPressGestureRecognizer!


override func viewDidLoad() {

    super.viewDidLoad()

    self.longPressGesture = UILongPressGestureRecognizer(target:self, action:"handleLongpress:")
    self.longPressGesture.minimumPressDuration = 2
    self.blueRec.addGestureRecognizer(self.longPressGesture)


}


func handleLongpress(sender:UILongPressGestureRecognizer) {

    print("LPress")

   switch (sender.state) {
    case UIGestureRecognizerState.Began:

        self.blueRec.center = sender.locationInView(self.graySuper)


        break;
    case UIGestureRecognizerState.Changed:

        self.blueRec.center = sender.locationInView(self.graySuper)



        break;
    case UIGestureRecognizerState.Ended:

        print("Press Ended ")

        break;
    default:
        break;
    } 

}

vapul
  • 77
  • 8
  • I edited the code above to include the solution. The pan gesture isn't necessary at all, as the long press gesture is continuos and can track the uiview's movement when monitoring state. – vapul Sep 27 '15 at 03:14
  • 1
    Possible duplicate of [Enable UIPanGestureRecognizer when did longPress](https://stackoverflow.com/questions/30014846/enable-uipangesturerecognizer-when-did-longpress) – Brian Tompsett - 汤莱恩 Jul 12 '18 at 16:20

1 Answers1

1

I'm answering my own question because it was a pretty basic solution that just eluded me at the moment. Hope this at least helps someone.

The long press gesture is enough to track the movement of the draggable object, and a separate pan gesture is not needed, as it appeared in my original code.

    @IBOutlet weak var graySuper: UIView!
@IBOutlet weak var blueRec: UIView!

@IBOutlet weak var blueLeading: NSLayoutConstraint!
@IBOutlet weak var blueTop: NSLayoutConstraint!

var longPressGesture:UILongPressGestureRecognizer!


override func viewDidLoad() {

    super.viewDidLoad()

    self.longPressGesture = UILongPressGestureRecognizer(target:self, action:"handleLongpress:")
    self.longPressGesture.minimumPressDuration = 2
    self.blueRec.addGestureRecognizer(self.longPressGesture)


}


func handleLongpress(sender:UILongPressGestureRecognizer) {

    print("LPress")

   switch (sender.state) {
    case UIGestureRecognizerState.Began:

        self.blueRec.center = sender.locationInView(self.graySuper)


        break;
    case UIGestureRecognizerState.Changed:

        self.blueRec.center = sender.locationInView(self.graySuper)



        break;
    case UIGestureRecognizerState.Ended:

        print("Press Ended ")

        break;
    default:
        break;
    } 
vapul
  • 77
  • 8
  • the thing i haven't figured out is the computations involved to keep the dragged object with the bounds of its superview. With the pan gesture, using translationInView is useful for that, but with the long press gesture, you have locationInView instead. I haven't figured out the restriction of the drag to its parent view using locationInView yet. – vapul Sep 27 '15 at 03:25
  • This works, but some uses (including mine) do need an actual UIPanGestureRecognizer in order to benefit what that class offers, ie. velocity support. If anybody has any thoughts on this, here is my use case: https://stackoverflow.com/questions/68663562/pan-view-using-uipangesturerecognizer-within-a-functional-uiscrollview/68670818#68670818 – Jeff C. Aug 06 '21 at 03:55