【问题标题】:pan gesture is not working for button in controller平移手势不适用于控制器中的按钮
【发布时间】:2021-08-09 08:57:39
【问题描述】:

我正在开发一个使用 UIGestureRecognizers 的应用程序 UI。我正在使用平移手势来移动控制器中的按钮。它不动。如果我调试它,我可以看到这个draggedButton 方法正在执行但气泡按钮没有移动。

我使用的代码是

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

【问题讨论】:

    标签: ios swift uipangesturerecognizer


    【解决方案1】:

    尝试添加:

    panGesture.cancelsTouchesInView = true
    

    【讨论】:

    • 完成。谢谢
    猜你喜欢
    • 2015-05-19
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多