Shake a View iOS
1 min readOct 5, 2020
--
While you are developing the iOS app there are many times we need to provide animation effect to capture the user interest so here is one the best example for the shaking UIView.
You can easily use the extension of UIView and just call the function.
extension UIView {
func shake() {let animation = CAKeyframeAnimation(keyPath: "transform.translation.x")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.duration = 0.6
animation.values = [-10.0, 10.0, -7.0, 7.0, -5.0, 5.0, 0.0 ]
layer.add(animation, forKey: "shake") }
}
This function can be used to draw attention to a specific view by shaking it a bit.