Skip to content

Instantly share code, notes, and snippets.

@mansi-27
Last active April 30, 2018 07:18
Show Gist options
  • Select an option

  • Save mansi-27/ee7aa9bcf5cc9ac5eb0f2ffa99e48f23 to your computer and use it in GitHub Desktop.

Select an option

Save mansi-27/ee7aa9bcf5cc9ac5eb0f2ffa99e48f23 to your computer and use it in GitHub Desktop.
Produce horizontal shake animation
extension UIView {
/// Produce horizontal shake animation
func shakeHorizontally(withCount count: Float = 4, duration: TimeInterval = 0.3, translation: Float = -10) {
let animation: CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.repeatCount = count
animation.duration = (duration)/TimeInterval(animation.repeatCount)
animation.autoreverses = true
animation.byValue = translation
layer.add(animation, forKey: "shake")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment