UIView shake animation
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding UIView Shake Animation
The `UIView` shake animation is a common visual effect used in iOS applications to indicate that the user needs to correct or provide additional input. Typically seen in login fields or text boxes when the input validation fails, the shake animation mimics the shaking of one's head to represent "no." This tutorial provides a comprehensive look at implementing and understanding this animation.
How the Shake Animation Works
Shake animation is essentially a translation on the `x-axis` that creates a back-and-forth movement. This is accomplished through a sequence of animations that shift the view's position. In practical terms, the animation often takes the form of a sinusoidal wave.
Implementing Shake Animation with Core Animation
To implement a shake animation, you can make use of Core Animation's `CAKeyframeAnimation`. This class provides the foundation for creating key-frame-based animations.
Code Example
Here's a basic implementation of the shake animation using Swift:
- CAKeyframeAnimation(keyPath:): This key path represents the property you want to animate—in this case, the `transform.translation.x`.
- timingFunction: Defines the pacing of an animation. `.linear` ensures a consistent pace across the animation sequence.
- values: An array indicating the positions the view will take during the animation represented by offsets from the original position.
- duration: Total time the animation takes to complete.
- Duration: Generally ranges from `0.2` to `0.5` seconds. A shorter duration makes the shake brisk, whereas a more extended duration results in a slower, deliberate shake.
- Path Length: Usually varies from `5.0` to `20.0` points. Smaller values produce a subtle effect, while larger values give a pronounced shake.
- Damping and Frequency: If you wish to exert more control over the animation, consider adding parameters for damping and frequency, which can be achieved by tweaking values in the `values` array.
- Direction: To implement a vertical shake, replace `translation.x` with `translation.y`.
Related reading
- UIView touch event in controller
- UIView touch event in controller
- UIView with rounded corners and drop shadow?
- UIViewContentModeScaleAspectFill not clipping
- UIViewController viewDidLoad vs. viewWillAppear What is the proper division of labor?
- UIView's border color in Interface builder doesn't work?
- UIWebView background is set to Clear Color, but it is not transparent
- UIWebView open links in Safari
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.