简体   繁体   中英

pan gesture is not working for button in controller

I am working on a application UI which uses UIGestureRecognizers. I am using the pan gesture to move the Button in the controller. It is not moving. If I debug it then I can see this draggedButton method is executing but bubble button is not moving.

The code that I am using is

private var panGesture = UIPanGestureRecognizer()

panGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedButton(sender:)))
        bubbleButton.isUserInteractionEnabled = true
        bubbleButton.addGestureRecognizer(panGesture)


   @objc func draggedButton(sender:UIPanGestureRecognizer) {
        self.view.bringSubview(toFront: bubbleButton)
        let translation = sender.translation(in: self.view)
        let xPostion = bubbleButton.center.x + translation.x
        let yPostion = bubbleButton.center.y + translation.y - bubbleButton.frame.height
        if (xPostion >= 0 && xPostion <= self.view.frame.size.width) &&  (yPostion >= 0 && yPostion <= self.view.frame.size.height)  {
            bubbleButton.center = CGPoint(x: bubbleButton.center.x + translation.x, y: bubbleButton.center.y + translation.y)
            sender.setTranslation(.zero, in: self.view)
        }
    }

Try adding:

panGesture.cancelsTouchesInView = true

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