我的一个观点是我的观点。手势不会让它向后移动并阻止它移过目标的中心。
如果用户拖过目标到达绿色侧,它将保持在中心位置,但一旦他们开始向按钮方向拖动,它就会退回。即使用户手指未超过XButton视图。
这里的代码示例:
let xGestureDrag = UIPanGestureRecognizer(target: self, action: #selector(AdViewPageVC.xButtonWasDragged(_:)))
@objc func xButtonWasDragged(_ sender: UIPanGestureRecognizer) {
if sender.state == UIGestureRecognizerState.began || sender.state == UIGestureRecognizerState.changed {
self.view.bringSubview(toFront: xButton)
let translation = sender.translation(in: self.view)
print(translation.x)
xButton.center = CGPoint(x: xButton.center.x + translation.x, y: xButton.center.y)
if xButton.center.x < startingPointxBtn {
xButton.center.x = startingPointxBtn
} else if xButton.center.x > largeCircle.center.x {
xButton.center.x = largeCircle.center.x
}
sender.setTranslation(CGPoint.zero, in: self.view)
} else if sender.state == UIGestureRecognizerState.ended {
if largeCircle.center.x - xButton.center.x <= 35.0 {
xButton.center = largeCircle.center
} else {
xButton.center.x = startingPointxBtn
}
}
}
视频示例: