我想为对象设置动画,所以我声明一个约束并将其添加到视图中。然后,我constant在UIView动画中更新约束的属性。为什么这段代码不移动对象?
constant
UIView
UIView.animateWithDuration(1, animations: { myConstraint.constant = 0 self.view.updateConstraints(myConstraint) })
为了声明动画,您不能重新定义约束并调用updateConstraints。您应该更改constant约束的,并遵循以下格式:
updateConstraints
self.view.layoutIfNeeded() UIView.animate(withDuration: 1) { self.sampleConstraint.constant = 20 self.view.layoutIfNeeded() }