Animating between two sets of Auto Layout constraints is quite simple.
All you have to do is update your installed/configured constraints and then call layoutIfNeeded
inside of a UIView.animateWith*
closure.
class DoorsViewController: UIViewController {
var open: Bool = false {
didSet { transition() }
}
func transition() {
self.view.layoutIfNeeded() // force layout before animating
UIView.animateWithDuration(0.4) {
// change constraints inside animation block
self.updateConstraintsForDoors()
// force layout inside animation block
self.view.layoutIfNeeded()
}
}
func updateConstraintsForDoors() {
leftDoorHorizontalConstraint.constant = open ? -16 : -leftDoorView.bounds.size.width
rightDoorHorizontalConstraint.constant = open ? -16 : -rightDoorView.bounds.size.width
}
}