Author: Sean Allen Video URL: How to Animate with Auto Layout Constraints Introduction: In this video we walk through how to create animations by adjusting the auto layout constraints and object, such as a UIImageView, UIButton, etc… This tutorial was done in Swift 4.2 and Xcode 10.
Add IBoutlet of constraints by ctrl+drag

Ways to animate with auto layout constraints
Add IBoutlet of the target constraint, and change its values, then update.
@IBOutlet weak var logoTopConstraint: NSLayoutConstraint!
func animateLogoTop() {
logoTopConstraint.constant = 50
UIView.animate(withDuration: 0.4) {
self.view.layoutIfNeeded()
}
}
Change the variable of programmed constraints and update
func animateStartButtonCenter() {
startButtonCenterYConstraint.constant = view.center.x
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 4, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
分类:iOS