平移手势后将视图返回到原始位置,迅速 [英] Returning view to original position after pan gesture, swift

查看:27
本文介绍了平移手势后将视图返回到原始位置,迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView(图像中的红色),我可以在 x 轴上平移.我可以将视图返回到原始位置,但我想返回到那个位置,就好像它被轻弹簧拉动一样,或者通过某种减速缓和到原始位置.

I have a UIView (the red one in the image) I can pan in the x-axis. I can return the view back to the original position but I want to return to that position as though it's being pulled by a light spring, or sort of easing back to the original position with some sort of deceleration.

此刻它奇怪地跳回到那个位置.

At the moment it weirdly jumps back to that position.

这是我的相关代码,并附有图像以显示其最大位置的视图.

This is my relevant code and an image is attached to show the view in it's maximum position.

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {

        if recognizer.state == UIGestureRecognizerState.Ended {

            print(backgroundView.center.x) //120.0
            print(backgroundView.center.y) //64.0

            let xDivision = Double(backgroundView.center.x/120.0)
            print(xDivision)

            recognizer.view?.center = CGPointMake(120.0, 64.0)

            self.view.frame = CGRectMake(backgroundView.center.x, 0, self.view.frame.width , self.view.frame.height)

            UIView.animateWithDuration(xDivision, delay: 0.0, usingSpringWithDamping: 2.0, initialSpringVelocity: 2.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {

                self.view.center.x = 120.0

                }, completion: { (true) in

                    print("Animation Complete")
                    print(self.backgroundView.center.x)
            })



        }

        let translation: CGPoint = recognizer.translationInView(self.view)
        //recognizer.view?.center = CGPointMake((recognizer.view?.center.x)!, (recognizer.view?.center.y)! + translation.y)
        recognizer.setTranslation(CGPointMake(0, 0), inView: self.view)


        var center: CGPoint = (recognizer.view?.center)!
        print(center.x)
        center.x += translation.x
        if center.x < 50.0 || center.x > 320.0 {
            return
        }
        recognizer.view?.center = center

    }

关于如何实现这一目标的任何想法.这个动画的一个很好的例子是这个应用程序 - http://rise.simplebots.co

Any ideas on how I can achieve that. A good example of this animation is this app - http://rise.simplebots.co

谢谢.

推荐答案

如果您使用自动布局,更改框架或中心属性可能会导致问题.你有没有想过只改变约束的常数?喜欢:

If you are using Auto Layout, changing the frame or center properties can cause problems. Have you thought about just changing the constraint's constant? Like:

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {

    switch recognizer.state {
    case .Began:
        //do something

    case .Changed:
        let translation = recognizer.translationInView(self.view)
        self.leftContraint.constant = self.leftContraint.constant + translation.x
        recognizer.setTranslation(.zero, inView: self.view)

    case .Ended:
        UIView.animateWithDuration(1,
            delay: 0,
            options: .CurveEaseOut,
            animations: {
                self.leftContraint.constant = //your value
                self.view.layoutIfNeeded()
            },
            completion: nil)

    default: ()
    }
}

这帮助我解决了类似的问题.

That helped me with a similar problem.

这篇关于平移手势后将视图返回到原始位置,迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆