UIPanrecognizer cancelled by subviews

51 Views Asked by At

I have this view -'card'- with UIPanrecognizer and an action attached to that.

Works as expected.

here's the action code:

  @IBAction func swipe(_ sender: UIPanGestureRecognizer) {
        let card = sender.view!
        let point = sender.translation(in: view)
        card.center = CGPoint(x: view.center.x + point.x, y: view.center.y + point.y)
        if sender.state == .ended{
            print("dropped")
            UIView.animate(withDuration: 0.2, animations: {card.center = self.view.center})
        }
    }

However, my view has several subviews.

it looks like this: enter image description here

If I start a panning gesture on one of the subviews, the gesture quite often misbehaves.

The misbehaviour I observe is that while I'm dragging around the view, when having initiated the gesture on a subview, the view doesn't consistently follow my finger, but just jumps back -at seemingly random moments and locations- to its original position. As you can see, I log when the gesture is ended. When the view jumps back, that log is not printed.

I have set User Interaction Enabled to false on all subviews

Is there a way to tell the subviews to pass the gesture to the view without interfering?

1

There are 1 best solutions below

0
Sjakelien On

While this is the answer, I'm not sure if I understand it:

One of the subviews, 'badge' had some auto layout constraints that caused the misbehavior.

I removed the constraints and redefined them in a more thoughtful way, and the problem disappeared.

I hope some of you can explain what happened. Thanks for all your involvement!

PS: reviewing the other constraints as well now...

enter image description here