Skip to content

Instantly share code, notes, and snippets.

@efremidze
Created May 5, 2017 22:10
Show Gist options
  • Save efremidze/027d1f00ce11f77b8e1e43a9bcd59e77 to your computer and use it in GitHub Desktop.
Save efremidze/027d1f00ce11f77b8e1e43a9bcd59e77 to your computer and use it in GitHub Desktop.
extension UIButton {
func addSpringAnimation() {
addTarget(self, action: #selector(scaleDown), for: [.touchDown, .touchDragEnter])
addTarget(self, action: #selector(scaleUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
}
@IBAction func scaleDown(_ button: UIButton) {
UIView.animate(withDuration: 0.1, delay: 0, options: [.allowUserInteraction, .curveLinear], animations: {
button.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}, completion: nil)
}
@IBAction func scaleUp(_ button: UIButton) {
if button.transform == .identity { return }
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.35, initialSpringVelocity: 0.7, options: [.allowUserInteraction, .curveLinear], animations: {
button.transform = .identity
}, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment