Skip to content

Instantly share code, notes, and snippets.

@ayoub-9
Created January 2, 2019 03:21
Show Gist options
  • Save ayoub-9/95f47aab6cf271c4cf0cbfce4e9434ee to your computer and use it in GitHub Desktop.
Save ayoub-9/95f47aab6cf271c4cf0cbfce4e9434ee to your computer and use it in GitHub Desktop.
extension UIButton {
func pulsate() {
let pulse = CASpringAnimation(keyPath: "transform.scale")
pulse.duration = 0.2
pulse.fromValue = 0.95
pulse.toValue = 1.0
pulse.autoreverses = true
pulse.repeatCount = 2
pulse.initialVelocity = 0.5
pulse.damping = 1.0
layer.add(pulse, forKey: "pulse")
}
func flash() {
let flash = CABasicAnimation(keyPath: "opacity")
flash.duration = 0.2
flash.fromValue = 1
flash.toValue = 0.1
flash.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
flash.autoreverses = true
flash.repeatCount = 3
layer.add(flash, forKey: nil)
}
func shake() {
let shake = CABasicAnimation(keyPath: "position")
shake.duration = 0.05
shake.repeatCount = 2
shake.autoreverses = true
let fromPoint = CGPoint(x: center.x - 5, y: center.y)
let fromValue = NSValue(cgPoint: fromPoint)
let toPoint = CGPoint(x: center.x + 5, y: center.y)
let toValue = NSValue(cgPoint: toPoint)
shake.fromValue = fromValue
shake.toValue = toValue
layer.add(shake, forKey: "position")
}
}
--------------------------------
Usage:
myButton.flash()
// myButton.pulsate()
// myButton.shake()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment