Created
December 22, 2020 17:58
-
-
Save BetterProgramming/b2a5142d2ecfa39793aaf61985525ed3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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