【发布时间】: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