我正在尝试在 2 个约束中设置优先级变化的动画,但我似乎无法让它发挥作用。我的动画代码是这样写的:
UIView.animate(withDuration: 0.2, animations: {
if self.cardHeaderBottomtoBodyTopConstraint.priority == UILayoutPriorityDefaultHigh {
self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultLow
self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultHigh
} else {
self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultHigh
self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultLow
}
self.flightInformationBodyCard.layoutIfNeeded()
self.flightInformationBodyCard.updateConstraints()
}, completion: nil)
最佳答案
我所要做的就是将 layoutIfNeeded 方法更改为 superview,如下所示:
if self.cardHeaderBottomtoBodyTopConstraint.priority == UILayoutPriorityDefaultHigh {
self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultLow
self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultHigh
} else {
self.cardHeaderBottomtoBodyTopConstraint.priority = UILayoutPriorityDefaultHigh
self.cardHeaderBottomToBodyBottomConstraint.priority = UILayoutPriorityDefaultLow
}
//self.flightInformationBodyCard.updateConstraints()
UIView.animate(withDuration: 0.2, animations: {
self.flightInformationBodyCard.superview?.layoutIfNeeded()
}, completion: nil)
关于ios - 约束动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424028/