Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implicit and Explicit Bounce Animation Button
extension UIButton {
func playImplicitBounceAnimation() {
let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
bounceAnimation.values = [1.0 ,1.4, 0.9, 1.15, 0.95, 1.02, 1.0]
bounceAnimation.duration = NSTimeInterval(0.5)
bounceAnimation.calculationMode = kCAAnimationCubic
layer.addAnimation(bounceAnimation, forKey: "bounceAnimation")
}
func playExplicitBounceAnimation() {
let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
var values = [Double]()
let e = 2.71
for t in 1..<100 {
let value = 0.6 * pow(e, -0.045 * Double(t)) * cos(0.1 * Double(t)) + 1.0
values.append(value)
}
bounceAnimation.values = values
bounceAnimation.duration = NSTimeInterval(0.5)
bounceAnimation.calculationMode = kCAAnimationCubic
layer.addAnimation(bounceAnimation, forKey: "bounceAnimation")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment