Skip to content

Instantly share code, notes, and snippets.

@mourad-brahim
Last active June 11, 2024 14:07
Show Gist options
  • Save mourad-brahim/cf0bfe9bec5f33a6ea66 to your computer and use it in GitHub Desktop.
Save mourad-brahim/cf0bfe9bec5f33a6ea66 to your computer and use it in GitHub Desktop.
Shake animation with swift
extension UIView {
func shake(duration: CFTimeInterval) {
let translation = CAKeyframeAnimation(keyPath: "transform.translation.x");
translation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
translation.values = [-5, 5, -5, 5, -3, 3, -2, 2, 0]
let rotation = CAKeyframeAnimation(keyPath: "transform.rotation.z")
rotation.values = [-5, 5, -5, 5, -3, 3, -2, 2, 0].map {
(let degrees: Double) -> Double in
let radians: Double = (M_PI * degrees) / 180.0
return radians
}
let shakeGroup: CAAnimationGroup = CAAnimationGroup()
shakeGroup.animations = [translation, rotation]
shakeGroup.duration = duration
self.layer.addAnimation(shakeGroup, forKey: "shakeIt")
}
}
@melih110
Copy link

Hocam kolay gelsin bu windowsta calisan uiview de nasil görüntülenir acaba

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment