Swift 为什么layoutIfNeeded()允许在更新约束时执行动画?
我有两种状态的Swift 为什么layoutIfNeeded()允许在更新约束时执行动画?,swift,layoutsubviews,animatewithduration,Swift,Layoutsubviews,Animatewithduration,我有两种状态的UIView: 我使用@IBaction按钮在它们之间设置动画: @IBAction func tapped(sender: UIButton) { flag = !flag UIView.animateWithDuration(1.0) { if self.flag { NSLayoutConstraint.activateConstraints([self.myConstraint]) } else {
UIView
:
我使用@IBaction
按钮在它们之间设置动画:
@IBAction func tapped(sender: UIButton) {
flag = !flag
UIView.animateWithDuration(1.0) {
if self.flag {
NSLayoutConstraint.activateConstraints([self.myConstraint])
} else {
NSLayoutConstraint.deactivateConstraints([self.myConstraint])
}
self.view.layoutIfNeeded() //additional line
}
}
仅当我添加其他行时,动画才起作用:
但当我删除该行时,我的UIView
会被更新,但没有任何动画,它会立即发生
为什么这条线会有如此大的不同?它是如何工作的?据我所知,更改约束不会立即更新视图的布局。它将更改排入队列,并且这些更改仅在系统下次更新布局时发生
通过将layoutIfNeeded放置在动画块中,可以强制在动画块期间应用视图更改。否则,它们将在设置动画之后发生