Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created December 22, 2020 17:58
Show Gist options
  • Save BetterProgramming/b2a5142d2ecfa39793aaf61985525ed3 to your computer and use it in GitHub Desktop.
Save BetterProgramming/b2a5142d2ecfa39793aaf61985525ed3 to your computer and use it in GitHub Desktop.
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")
}
//Use like this:
someView.shake()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment