简体   繁体   English

在2个固定点之间滑动UIView

[英]Sliding UIView between 2 fixed points

I'm trying to let a UIView slide between 2 points on slide touch. 我试图让UIView幻灯片在2点之间滑动。 I've tried using UIPanGestureRecognizer. 我尝试使用UIPanGestureRecognizer。 The sliding is fine but when the view arrive at the limit point, there's a bumpy jump. 滑动很好,但是当视图到达极限点时,会有颠簸的跳动。 It's not smooth. 这不顺利。 This is my gesture function: 这是我的手势功能:

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


}

Any help? 有什么帮助吗? Thanks. 谢谢。

Your problem is that you are looking at where the view is now, and not where it will be after the current move has been applied. 您的问题是您正在查看视图的当前位置,而不是在应用当前移动后的视图位置。

I fixed your code by introducing newx which shows where the view is trying to move to. 我通过引入newx修复了您的代码,其中newx显示了视图试图移动到的位置。 That is the value that you want to constrain. 那就是您要限制的值。

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

You might also want to consider a change to your final setTranslation call. 您可能还想考虑对最终setTranslation调用的更改。 If the user tries to drag the view left when it is already at its left limit and then (without lifting their finger) changes direction and moves to the right, your code will move the view right even though their finger is no longer over the view. 如果用户试图在视图已达到其左极限时向左拖动,然后(不松开手指)改变方向并向右移动,即使他们的手指不再在视图上,您​​的代码也会将视图向右移动。

With the change below, the view will not move to the right until the user's finger gets back to the original touch point: 进行以下更改后,视图将不会向右移动,直到用户的手指回到原始触摸点为止:

gestureRecognizer.setTranslation(CGPoint(x: newx - gestureRecognizer.view!.center.x ,y: 0), in: self.view)

Try it and see which you prefer. 试试看,看看你喜欢哪个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM