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

带约束的UIViewAnimation在Swift中不起作用,swift,uiview,uiviewanimation,Swift,Uiview,Uiviewanimation,我正在尝试创建一个动画,在加载父UIViewController后,我希望将uiview从右向左滑动,尽管我可以显示视图,但动画不起作用。我的动画代码: override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) view.isOpaque = false UIView.animate(withDuration: 1.0, delay: 1.0, options: .curv

我正在尝试创建一个动画,在加载父UIViewController后,我希望将uiview从右向左滑动,尽管我可以显示视图,但动画不起作用。我的动画代码:

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
    }) { (status) in

    }
}
我已将父视图控制器显示为显示的视图控制器:

present(navController, animated: false, completion: nil)

将动画代码从ViewDidDisplay移动到ViewDidLayoutSubview

您忘记了
self.view.layoutFneed()

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


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

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

    }
}

@潘卡吉很乐意帮忙:)
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

    }
}