简体   繁体   中英

How do I enable a single touch to handle both UILongPressGestureRecognizer and UIPanGestureRecognizer?

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;
    } 

}

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;
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM