You can use the spring animation method that is built into the UIView:
func toggleBar() -> Void {
self.view.layoutIfNeeded()
let newHeight:CGFloat = !barShut ? 30:100
barShut = !barShut
barHeightConstraint.constant = newHeight
UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3, options: [], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
You will need a longer animation duration than 0.15 seconds to make the rebound seem realistic; I think the values ββthat I look pretty good, but you can play with them to get the exact effect that you are after.
, , , /, . barShut , . , .