动画约束快速适合所有屏幕

时间:2016-06-20 07:43:42

标签: ios swift animation

我是一个受约束的初学者,最近我开始研究动画,我的目标是iPhone 6s plus。这一切都是关于将球从某个Y点移动到另一个点。

  func animateBall(){
        UIView.animateWithDuration(0.5, delay: 0.0000001, options : [.CurveEaseInOut, .TransitionCurlDown,] , animations: {

        self.ball1.frame.origin.y = 121.0
        self.ball2.frame.origin.y = 175.0
        self.ball3.frame.origin.y = 340.0
        self.ball4.frame.origin.y = 394.0
        self.ball5.frame.origin.y = 447.0
        self.ball6.frame.origin.y = 502.0
        self.ball7.frame.origin.y = 555.0
        self.ball8.frame.origin.y = 585.0
        self.ball9.frame.origin.y = 68.0

        //
        //            }
        //

        } ,completion :{(finished:Bool) in
            // the code you put here will be compiled once the animation finishes
            self.restoreToNormalBalls()


    })
}



 func restoreToNormalBalls (){

    self.ball1.frame.origin.y = 68
    self.ball2.frame.origin.y = 121
    self.ball3.frame.origin.y = 175
    self.ball4.frame.origin.y = 340
    self.ball5.frame.origin.y = 394
    self.ball6.frame.origin.y = 447
    self.ball7.frame.origin.y = 502
    self.ball8.frame.origin.y = 555
    self.ball9.frame.origin.y = 15

}

我已经制定了适合所有屏幕的约束,但是当我点击动画时,它仅适用于6s Plus。任何帮助使动画在所有屏幕上都有效? 感谢

2 个答案:

答案 0 :(得分:1)

根据@lubilis的建议,

首先,从球到其容器视图的上边缘添加顶部空间约束 然后将该约束设为@IBOutlet,让我们说outletedConstraintBall1 然后在制作动画时,请执行以下操作:

UIView.animateWithDuration(0.5, delay: 0.0000001, options : [.CurveEaseInOut, .TransitionCurlDown,] , animations: {

    outletedConstraintBall1.constant = 121
    containerView.layoutIfNeeded()
})

重置它的位置:

restoreToNormalBalls() {
     outletedConstraintBall1.constant = 68
}

答案 1 :(得分:0)

如果您正在使用自动布局,则应设置约束常量而不是帧:

func animateBall() {

    ball1Constraint.constant = 121.0;
    ball2Constraint.constant = 175.0;

    UIView.animateWithDuration(0.5, delay: 0.0000001, options : [.CurveEaseInOut, .TransitionCurlDown,] , animations: {

        self.view.layoutIfNeeded() // called on parent view

    } ,completion :{ (finished:Bool) in

        // the code you put here will be compiled once the animation finishes
        self.restoreToNormalBalls() // remember to modify this implementation method using constraints too
    })
}
  • ball1Constraint& ball2Constraint是从Storyboard / Xib文件连接为@IBOutlet的约束