0
votes

I have the following code that drags a UIView. All works fine visually.

func moveView(sender: UIPanGestureRecognizer) {
        let translate = sender.translationInView(self.view)
        if sender.state == UIGestureRecognizerState.Changed {
            sender.view!.center = CGPoint(x:sender.view!.center.x + translate.x, y:sender.view!.center.y + translate.y)
            sender.setTranslation(CGPointZero, inView: self.view)
        }
        if sender.state == UIGestureRecognizerState.Ended {
            let newX: CGFloat = sender.view!.center.x + translate.x
            let newY: CGFloat = sender.view!.center.y + translate.y
            sender.view!.center = CGPoint(x:newX, y:newY)
        }
}

However after completing this drag, the view seems to lose the gesture connection such that I can't drag it again or trigger any tap gesture associated with it etc.

If I add an NSLog I can see that tapping where the view used to be triggers the log but not if I tap on the actual current view location.

I establish the gesture to view thisView with the following within viewDidLoad

let moveGesture = UIPanGestureRecognizer(target: self, action: Selector("moveView:"))
thisView.addGestureRecognizer(moveGesture)

What am I missing that keeps the gestures connected to the new view location?

Thanks.

1

1 Answers

1
votes

I think that when you call this sender.setTranslation(CGPointZero, inView: self.view) maybe you should set the translation relative to superview and not to the view itself. Setting the translation relative to itself change the position of the view contents but not the view area, which means that if you set the view layer to mask to its bounds you shouldn't see anything that it's outside of the view initial area.
So you should do sender.setTranslation(CGPointZero, inView: self.view.superview).