Spring animations make movements in your app look smooth and natural, like things bouncing or settling gently.
Spring animations in iOS Swift
UIView.animate(withDuration: TimeInterval, delay: TimeInterval, usingSpringWithDamping: CGFloat, initialSpringVelocity: CGFloat, options: UIView.AnimationOptions, animations: @escaping () -> Void, completion: ((Bool) -> Void)?)
usingSpringWithDamping controls how bouncy the animation is. Values close to 1 mean less bounce.
initialSpringVelocity sets the speed at which the animation starts.
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [], animations: { view.center.y += 100 }, completion: nil)
UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: [], animations: { view.alpha = 0 }, completion: { finished in view.isHidden = true })
This app shows a blue square. When you tap it, the square grows bigger with a spring bounce, then returns to normal size with another spring animation.
import UIKit class ViewController: UIViewController { let box = UIView() override func viewDidLoad() { super.viewDidLoad() box.frame = CGRect(x: 100, y: 100, width: 100, height: 100) box.backgroundColor = .systemBlue view.addSubview(box) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(animateBox)) box.addGestureRecognizer(tapGesture) box.isUserInteractionEnabled = true } @objc func animateBox() { UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.8, options: [], animations: { self.box.transform = CGAffineTransform(scaleX: 1.5, y: 1.5) }, completion: { _ in UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: [], animations: { self.box.transform = .identity }, completion: nil) }) } }
Spring animations feel more natural than simple linear animations.
Adjust usingSpringWithDamping and initialSpringVelocity to get the bounce you want.
Remember to keep animations short to keep the app feeling responsive.
Spring animations add bounce and life to your app's movements.
Use UIView.animate with spring parameters for smooth effects.
Experiment with damping and velocity to find the right feel.
Practice
dampingRatio parameter control in a spring animation?Solution
Step 1: Understand dampingRatio meaning
ThedampingRatiocontrols the amount of oscillation or bounce in the animation.Step 2: Relate dampingRatio to bounce effect
A lower damping ratio means more bounce; a higher ratio means less bounce.Final Answer:
How much the animation bounces before settling -> Option AQuick Check:
dampingRatio = bounce amount [OK]
- Confusing dampingRatio with animation duration
- Thinking dampingRatio sets animation delay
- Assuming dampingRatio changes view color
Solution
Step 1: Recall UIView spring animation method
The correct method isUIView.animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:).Step 2: Match parameters and syntax
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: [], animations: { /* changes */ }, completion: nil) matches the exact method signature and parameter names used in Swift.Final Answer:
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: [], animations: { /* changes */ }, completion: nil) -> Option AQuick Check:
Correct method name and parameters = UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: [], animations: { /* changes */ }, completion: nil) [OK]
- Using incorrect method names like animateSpring or springAnimation
- Missing required parameters or using wrong parameter names
- Confusing options parameter with damping or velocity
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 0, options: [], animations: {
view.frame.origin.y += 100
}, completion: nil)Solution
Step 1: Analyze dampingRatio value
The damping ratio is 0.2, which is low, so the animation will bounce a lot.Step 2: Understand animation effect
The view's y position increases by 100, so it moves down with a strong bounce.Final Answer:
The view moves down with a strong bounce effect -> Option BQuick Check:
Low dampingRatio = strong bounce [OK]
- Assuming low damping means no bounce
- Thinking initialSpringVelocity 0 stops movement
- Confusing frame change with no animation
UIView.animate(withDuration: 1.0, delay: 0, usingSpringWithDamping: -0.5, initialSpringVelocity: 0.5, options: [], animations: {
view.alpha = 0
}, completion: nil)Solution
Step 1: Check dampingRatio valid range
TheusingSpringWithDampingparameter must be greater than or equal to 0.Step 2: Identify invalid value
Here, -0.5 is invalid and will cause the animation to diverge or behave incorrectly.Final Answer:
usingSpringWithDamping cannot be less than 0 -> Option DQuick Check:
dampingRatio >= 0 [OK]
- Setting dampingRatio below 0
- Thinking initialSpringVelocity must be zero
- Believing alpha cannot be animated
Solution
Step 1: Understand desired animation traits
A quick start means high initial velocity; gentle bounce means high damping ratio near 1.Step 2: Match parameters to traits
usingSpringWithDamping: 0.8, initialSpringVelocity: 3.0 has damping 0.8 (gentle bounce) and velocity 3.0 (quick start), fitting the description.Final Answer:
usingSpringWithDamping: 0.8, initialSpringVelocity: 3.0 -> Option CQuick Check:
High damping + high velocity = gentle bounce, quick start [OK]
- Using low damping for gentle bounce
- Setting velocity too low for quick start
- Confusing damping and velocity roles
Ready to prove it?
Pass the test and this pattern moves unknown
