iOS - 动画背景问题

时间:2017-12-05 11:35:48

标签: ios iphone swift animation uiviewanimation

我正在尝试在屏幕上制作一些元素动画,这里是代码:

  private func setupAnimations() {

    self.emailAddress.alpha = 0
    self.fullName.alpha = 0
    self.password.alpha = 0
    self.confirmPassword.alpha = 0
    self.signUp.alpha = 0
    self.fullName.leftView?.alpha = 0
    self.emailAddress.leftView?.alpha = 0
    self.password.leftView?.alpha = 0
    self.confirmPassword.leftView?.alpha = 0

    self.emailAddress.center.x += self.view.bounds.width-10
    self.fullName.center.x -= self.view.bounds.width-10
    self.password.center.x -= self.view.bounds.width-10
    self.confirmPassword.center.x += self.view.bounds.width-10
    self.signUp.center.y += self.view.bounds.height/2

    UIView.animate(withDuration: 1.7, delay: 0.2, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.allowUserInteraction,.curveEaseInOut], animations: {

        self.emailAddress.alpha = 1
        self.emailAddress.center.x -= self.view.bounds.width-10
        self.view.layoutIfNeeded()

    }, completion: {(completed) in

        if completed {

            UIView.animate(withDuration: 1, animations: {
                self.fullName.leftView?.alpha = 1
                self.emailAddress.leftView?.alpha = 1
                self.password.leftView?.alpha = 1
                self.confirmPassword.leftView?.alpha = 1
            })

        }

    })

    UIView.animate(withDuration: 1.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [.allowUserInteraction,.curveEaseInOut], animations: {

        self.fullName.alpha = 1
        self.fullName.center.x += self.view.bounds.width-10
        self.view.layoutIfNeeded()

    }, completion: {(completed) in

        if completed {

            UIView.animate(withDuration: 1, animations: {
                self.fullName.leftView?.alpha = 1
                self.emailAddress.leftView?.alpha = 1
                self.password.leftView?.alpha = 1
                self.confirmPassword.leftView?.alpha = 1
            })

        }

    })

依此类推......你明白了 - 我将alpha设置为0,将x设置为屏幕外,并设置动画效果"到达"在屏幕上,它在viewDidLoad()中被调用。一切都工作了一个月,但最近我检查了这个,问题就在这里:

视频链接:https://streamable.com/lhqvu

背景,即超视图中的UIImage也正在动画,使得超级视图(红色背景)被看到。

screenshot of hierarchy and constraints

任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

说到动画,你应该在viewDidAppear而不是viewDidLoad上调用该函数。

答案 1 :(得分:0)

setupAnimations中添加动画viewWillAppear方法,并从self.view.layoutIfNeeded()块中删除usingSpringWithDamping。在故事板中取消选中 safeArea ,并在为superview设置顶级约束时取消选中 约束到边距 &设置为零。