iOS - 动画背景问题

iOS - Animating background issue

我正在尝试为屏幕上的几个元素设置动画,代码如下:

  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 设置为屏幕外,并在屏幕上对其进行动画处理 "arriving",它在 viewDidLoad() 中被调用。一个月一切顺利,但最近我检查了这个,这就是问题所在:

视频 link:https://streamable.com/lhqvu

超级视图中的 UIImage 背景也被动画化,使超级视图(红色背景)可见。

如有任何帮助,我们将不胜感激。提前谢谢你。

关于动画,您应该在 viewDidAppear 而不是 viewDidLoad 上调用函数。

viewWillAppear 中添加动画 setupAnimations 方法并从 usingSpringWithDamping 块中删除 self.view.layoutIfNeeded()。在情节提要中取消选中 safeArea 并在将顶部约束设置为超级视图时取消选中 Constrain to margins & 置零。