添加带动画的约束查看
Add Constraint with animation to view
嘿,我正在尝试通过使用此代码更改其约束来引入视图
UIView.animate(withDuration: 500, animations: {
self.forgetTopConstraint.isActive = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
})
forgetTopConstraint
是一个约束条件,它将 forgetPasswordView
的顶部锚定到 view
的底部,常量为 0,因此 forgetTopConstraint
不可见,那么我是尝试使用该代码将其动画化到 view
的中心,但它突然出现而不是动画
在没有看到更多周围代码或视图设置的情况下,听起来您只需要在包含视图上调用 layoutIfNeeded
。这是实际执行应用更新约束的工作的方法。 layoutIfNeeded
由视图的许多其他更改自动触发,但在希望它动画的情况下,您实际上不需要在动画块内调用任何其他布局更改。只需调用一个语句,它就会使您之前的所有更改都动画化:
self.forgetTopConstraint.isActive = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
UIView.animate(withDuration: 500, animations: {
self.view.layoutIfNeeded()
})
嘿,我正在尝试通过使用此代码更改其约束来引入视图
UIView.animate(withDuration: 500, animations: {
self.forgetTopConstraint.isActive = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
})
forgetTopConstraint
是一个约束条件,它将 forgetPasswordView
的顶部锚定到 view
的底部,常量为 0,因此 forgetTopConstraint
不可见,那么我是尝试使用该代码将其动画化到 view
的中心,但它突然出现而不是动画
在没有看到更多周围代码或视图设置的情况下,听起来您只需要在包含视图上调用 layoutIfNeeded
。这是实际执行应用更新约束的工作的方法。 layoutIfNeeded
由视图的许多其他更改自动触发,但在希望它动画的情况下,您实际上不需要在动画块内调用任何其他布局更改。只需调用一个语句,它就会使您之前的所有更改都动画化:
self.forgetTopConstraint.isActive = false
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.forgetPasswordView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0))
UIView.animate(withDuration: 500, animations: {
self.view.layoutIfNeeded()
})