在 Swift 中以编程方式更新约束的常量 属性?

Update the constant property of a constraint programmatically in Swift?

我想为一个对象设置动画,所以我声明了一个约束并将其添加到视图中。然后我更新 UIView 动画中约束的 constant 属性。为什么此代码不移动对象?

UIView.animateWithDuration(1, animations: {
     myConstraint.constant = 0   
     self.view.updateConstraints(myConstraint)
})

为了声明动画,不能重新定义约束并调用updateConstraints。您应该更改约束的 constant 并遵循以下格式:

self.view.layoutIfNeeded()
UIView.animate(withDuration: 1) {
    self.sampleConstraint.constant = 20
    self.view.layoutIfNeeded()
}