我有两种状态的UIView: 我使用@IBaction for button在它们之间有动画: @IBAction func tapped(sender: UIButton) { flag = !flag UIView.animateWithDuration(1.0) { if self.flag { NSLayoutConstraint.activateConstraints([self.myC
我使用@IBaction for button在它们之间有动画:
@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放在动画块中,它会强制在动画块期间应用视图更改.否则它们会在动画设置完成后发生.