代碼如下, 當你修改完約束之后添加動畫之前, 你需要事先調用一次self.view.layoutIfNeeded
.
它能確保在動畫過程之中只有你修改的約束,會被加上動畫效果.
否者其他view發生變化的時候也會被這個動畫影響.
...
self.view.layoutIfNeeded()
topInfosViewHeight.constant = 102
UIView.animateWithSpring({
self.view.layoutIfNeeded()
})
...
extension UIView {
class func animateWithSpring(animate: ()->Void) {
animateWithDuration(0.7, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.7, options: .LayoutSubviews, animations: animate, completion: nil)
}
}
Referencer: Best practice for modifying storyboard auto layout constraints in code