UIView.animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:) with a damping value below 1.0 (e.g., 0.6) for a simple bounce. For physics‑based bounces that interact with boundaries, add a UIDynamicAnimator with gravity, collision, and elasticity behaviors.Why this happens
UIKit does not provide a dedicated “bounce” animation type. The built‑in CATransition push used in the question only slides a layer in a single direction. To get a bounce — where the view overshoots its final position and settles back — you must either simulate a spring with damping or run a tiny physics engine. Both approaches have shipped since iOS 7 and remain fully supported in iOS 17.
How to fix it
1. Spring‑damping animation (simplest, most common)
UIView.animate(withDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:) lets you define a damped harmonic oscillator. A usingSpringWithDamping value between 0.0 and 1.0 controls bounciness; lower values bounce more. initialSpringVelocity sets the initial speed as “total animation distance per second.”
Swift
func bounceIn(view: UIView, fromTopOf superview: UIView) {
// Start off‑screen
view.center.y = -view.bounds.height / 2
superview.addSubview(view)
UIView.animate(
withDuration: 0.6,
delay: 0,
usingSpringWithDamping: 0.6, // 0.0 = very bouncy, 1.0 = no overshoot
initialSpringVelocity: 0.8, // ≈ distance / desired seconds
options: [.curveEaseInOut],
animations: {
view.center.y = superview.bounds.midY
},
completion: nil
)
}
Objective‑C
- (void)bounceInView:(UIView *)view fromTopOf:(UIView *)superview {
view.center = CGPointMake(CGRectGetMidX(superview.bounds),
-CGRectGetHeight(view.bounds) / 2);
[superview addSubview:view];
[UIView animateWithDuration:0.6
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:0.8
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
view.center = CGPointMake(CGRectGetMidX(superview.bounds),
CGRectGetMidY(superview.bounds));
}
completion:nil];
}
Tip: If the view should land at a specific frame instead of the center, animate frame or transform inside the animations block.
2. UIKit Dynamics (physics‑based, interactive)
When you want the bounce to respect screen edges or other views, use UIDynamicAnimator. The pattern is always the same:
- Keep a strong reference to the animator.
- Add behaviors (gravity, collision, elasticity) for each animated item.
- Ensure the item’s starting state is valid inside the reference view.
Swift
final class BounceController {
private let animator: UIDynamicAnimator
private let item: UIDynamicItem
init(referenceView: UIView, item: UIDynamicItem) {
self.animator = UIDynamicAnimator(referenceView: referenceView)
self.item = item
configureBehaviors()
}
private func configureBehaviors() {
// Gravity pulls down
let gravity = UIGravityBehavior(items: [item])
gravity.magnitude = 1.0
animator.addBehavior(gravity)
// Collision with reference view bounds
let collision = UICollisionBehavior(items: [item])
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collision)
// Elasticity creates the bounce
let elasticity = UIDynamicItemBehavior(items: [item])
elasticity.elasticity = 0.7 // 0.0 = no bounce, 1.0 = perfect bounce
animator.addBehavior(elasticity)
}
}
// Usage
let scoreView = UIView(frame: CGRect(x: 100, y: -100, width: 200, height: 100))
view.addSubview(scoreView)
_ = BounceController(referenceView: view, item: scoreView)
Objective‑C
@interface BounceController : NSObject
@property (nonatomic, strong) UIDynamicAnimator *animator;
- (instancetype)initWithReferenceView:(UIView *)refView item:(id<UIDynamicItem>)item;
@end
@implementation BounceController
- (instancetype)initWithReferenceView:(UIView *)refView item:(id<UIDynamicItem>)item {
self = [super init];
if (self) {
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:refView];
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[item]];
gravity.magnitude = 1.0f;
[_animator addBehavior:gravity];
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[item]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[_animator addBehavior:collision];
UIDynamicItemBehavior *elasticity = [[UIDynamicItemBehavior alloc] initWithItems:@[item]];
elasticity.elasticity = 0.7f;
[_animator addBehavior:elasticity];
}
return self;
}
@end
Choose Dynamics when the bounce must react to other moving views or when you plan to add user interaction (e.g., dragging the view).
How to prevent it
- Keep a strong reference to the
UIDynamicAnimator; ARC will deallocate it otherwise and the animation stops instantly. - Start the view in a valid position inside the reference bounds (or just outside) so the physics simulation has a legal initial state.
- For spring damping, avoid
usingSpringWithDamping: 0— it creates an infinite oscillation. - Test on device; simulator physics can run at a different speed than hardware.
FAQ
Can I combine spring damping with Auto Layout?
Yes. Animate layoutIfNeeded() inside the animations block after updating constraints, or animate a view’s transform property which does not affect layout.
Why does my Dynamics animation stop immediately?
The UIDynamicAnimator was likely stored in a weak variable or local constant. Hold it in a retained property so the simulation stays alive.
Which approach performs better?
Spring damping is a single Core Animation transaction — negligible overhead. Dynamics runs a small physics step each frame; it’s still lightweight but avoid hundreds of simultaneous dynamic items on older devices.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.