在 2 个固定点之间滑动 UIView
Sliding UIView between 2 fixed points
我试图让 UIView 在滑动触摸时在 2 个点之间滑动。我试过使用 UIPanGestureRecognizer。滑动很好,但是当视图到达极限点时,会有一个颠簸的跳跃。这并不顺利。这是我的手势功能:
func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.began || gestureRecognizer.state == UIGestureRecognizerState.changed {
let translation = gestureRecognizer.translation(in: self.view)
print(gestureRecognizer.view!.center.x)
if(gestureRecognizer.view!.center.x <= self.view.frame.width) && (gestureRecognizer.view!.center.x >= self.view.frame.width - 100) {
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y)
}else if (gestureRecognizer.view!.center.x > self.view.frame.width){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width, y: gestureRecognizer.view!.center.y)
}else if (gestureRecognizer.view!.center.x < self.view.frame.width - 100){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width - 100, y: gestureRecognizer.view!.center.y)
}
gestureRecognizer.setTranslation(CGPoint(x: 0,y: 0), in: self.view)
}
}
有什么帮助吗?谢谢
您的问题是您查看的是视图现在所在的位置,而不是应用当前移动后视图所在的位置。
我通过引入 newx
修复了您的代码,它显示了视图试图移动到的位置。这就是您要约束的值。
@IBAction func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
print(gestureRecognizer.view!.center.x)
let newx = gestureRecognizer.view!.center.x + translation.x
if(newx <= self.view.frame.width) && (newx >= self.view.frame.width - 100) {
gestureRecognizer.view!.center = CGPoint(x: newx, y: gestureRecognizer.view!.center.y)
}else if (newx > self.view.frame.width){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width, y: gestureRecognizer.view!.center.y)
}else if (newx < self.view.frame.width - 100){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width - 100, y: gestureRecognizer.view!.center.y)
}
gestureRecognizer.setTranslation(CGPoint(x: 0,y: 0), in: self.view)
}
}
您可能还想考虑更改最后的 setTranslation
通话。如果用户试图在视图已经在其左侧限制时向左拖动视图,然后(不抬起手指)改变方向并向右移动,即使他们的手指不再位于视图上方,您的代码也会将视图向右移动.
通过以下更改,在用户的手指回到原始触摸点之前,视图不会向右移动:
gestureRecognizer.setTranslation(CGPoint(x: newx - gestureRecognizer.view!.center.x ,y: 0), in: self.view)
试试看你喜欢哪个。
我试图让 UIView 在滑动触摸时在 2 个点之间滑动。我试过使用 UIPanGestureRecognizer。滑动很好,但是当视图到达极限点时,会有一个颠簸的跳跃。这并不顺利。这是我的手势功能:
func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == UIGestureRecognizerState.began || gestureRecognizer.state == UIGestureRecognizerState.changed {
let translation = gestureRecognizer.translation(in: self.view)
print(gestureRecognizer.view!.center.x)
if(gestureRecognizer.view!.center.x <= self.view.frame.width) && (gestureRecognizer.view!.center.x >= self.view.frame.width - 100) {
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y)
}else if (gestureRecognizer.view!.center.x > self.view.frame.width){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width, y: gestureRecognizer.view!.center.y)
}else if (gestureRecognizer.view!.center.x < self.view.frame.width - 100){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width - 100, y: gestureRecognizer.view!.center.y)
}
gestureRecognizer.setTranslation(CGPoint(x: 0,y: 0), in: self.view)
}
}
有什么帮助吗?谢谢
您的问题是您查看的是视图现在所在的位置,而不是应用当前移动后视图所在的位置。
我通过引入 newx
修复了您的代码,它显示了视图试图移动到的位置。这就是您要约束的值。
@IBAction func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
print(gestureRecognizer.view!.center.x)
let newx = gestureRecognizer.view!.center.x + translation.x
if(newx <= self.view.frame.width) && (newx >= self.view.frame.width - 100) {
gestureRecognizer.view!.center = CGPoint(x: newx, y: gestureRecognizer.view!.center.y)
}else if (newx > self.view.frame.width){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width, y: gestureRecognizer.view!.center.y)
}else if (newx < self.view.frame.width - 100){
gestureRecognizer.view!.center = CGPoint(x: self.view.frame.width - 100, y: gestureRecognizer.view!.center.y)
}
gestureRecognizer.setTranslation(CGPoint(x: 0,y: 0), in: self.view)
}
}
您可能还想考虑更改最后的 setTranslation
通话。如果用户试图在视图已经在其左侧限制时向左拖动视图,然后(不抬起手指)改变方向并向右移动,即使他们的手指不再位于视图上方,您的代码也会将视图向右移动.
通过以下更改,在用户的手指回到原始触摸点之前,视图不会向右移动:
gestureRecognizer.setTranslation(CGPoint(x: newx - gestureRecognizer.view!.center.x ,y: 0), in: self.view)
试试看你喜欢哪个。