带有约束的UIViewAnimation在Swift中不起作用

时间:2018-07-28 11:55:00

标签: swift uiview uiviewanimation

我正在尝试创建一个动画,在此动画中,一旦加载了父UIViewController,我想从右向左滑动uiview,尽管我能够显示视图,但动画无法正常工作。我的动画代码:

Sized

我已将父视图控制器显示为提供的viewcontroller:

'static

3 个答案:

答案 0 :(得分:0)

将动画代码从viewDidAppear移至viewDidLayoutSubviews

答案 1 :(得分:0)

您忘记了self.view.layoutIfNeeded()

self.contentXConstraint.constant = 500
self.contentXConstraint.constant = 80

UIView.animate(withDuration: 1.0, delay: 1.0, options: .curveEaseOut, animations: {
   self.view.layoutIfNeeded()
}) { (status) in
}

来自https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded

  

使用此方法强制视图立即更新其布局。使用自动版式时,版式引擎会根据需要更新视图的位置,以满足约束的变化

答案 2 :(得分:0)

更新约束后,您需要添加layoutIfNeeded()方法

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    view.isOpaque = false
    UIView.animate(withDuration: 1.0, delay: 1.0, options: .curveEaseOut, animations: {
        self.contentXConstraint.constant = 500
        self.contentXConstraint.constant = 80
        self.view.layoutIfNeeded()
    }) { (status) in

    }
}