UIKit Dynamics is a physical animation framework in iOS (UIKit) that allows Views to interact according to real-world physics laws: gravity, collisions, elasticity and attraction forces. This article explains the components of UIDynamicAnimator, types of behaviors, and practical use cases.
Key Takeaways
UIKit Dynamics is a physics simulation framework introduced by Apple in iOS 7 (2013). Unlike UIView.animate (linear transitions) and Core Animation (layer animation), UIKit Dynamics simulates real physical interactions: gravity, collisions, forces, elasticity and friction. The developer adds behaviors to UIDynamicAnimator, and the system automatically calculates physics for each frame.
The framework runs at 60/120 FPS and uses a built-in physics engine based on rigid body simulation. UIKit Dynamics does not require knowledge of physics or mathematics — just apply a ready-made behavior and configure its parameters.
According to Apple, UIKit Dynamics is suitable for UI effects (card stack animation, icon wobbling, interface physics), but is not intended for games — use SceneKit or SpriteKit for games.
UIDynamicAnimator is the central class of UIKit Dynamics. It initializes with a reference view (usually the controller's view) and manages all active behaviors. Each frame UIDynamicAnimator recalculates positions, velocities and accelerations of all added elements according to physical laws.
The addBehavior method adds one behavior (or a complex UIDynamicBehavior), removeBehavior removes it. The animator automatically pauses when all objects reach a state of rest (continue without collisions and forces). The delegate method — UIDynamicAnimatorDelegate — notifies about pause and resumption of the simulation.
Any object participating in physics must implement the UIDynamicItem protocol. UIView and UICollectionViewLayoutAttributes already implement it. The protocol requires properties: bounds, center, transform. If the object is custom — implement the protocol manually.
UIKit Dynamics includes seven built-in behavior types, each modeling a specific physical effect. Behaviors can be combined: for example, gravity + collisions + elasticity create the effect of falling and bouncing objects.
| Behavior | Physical Effect | Key Parameters |
|---|---|---|
| UIGravityBehavior | Gravity (force vector) | gravityDirection (dx, dy), magnitude |
| UICollisionBehavior | Object and boundary collisions | collisionMode, translucence, collisionDelegate |
| UIAttachmentBehavior | Connection (spring/cable) | anchorPoint, length, damping, frequency |
| UISnapBehavior | Snap to point with damping | snapPoint, damping (0-1) |
| UIPushBehavior | Push/impulse (continuous or instantaneous) | pushDirection, magnitude, mode |
| UIDynamicItemBehavior | Object physical properties | elasticity, friction, density, resistance |
| UIFieldBehavior | Force fields (vortex, magnetic, radial) | position, strength, falloff, region |
For realistic object physics, multiple behaviors are needed simultaneously. For example, for a falling card: UIGravityBehavior (gravity force), UICollisionBehavior (collision with the bottom), UIDynamicItemBehavior (elasticity: 0.4 for bounce). All behaviors are added to one UIDynamicAnimator.
Swift code for UIKit Dynamics creates a UIDynamicAnimator and adds behaviors. The example below demonstrates gravity with multiple objects, collisions and custom physical properties.
import UIKit
class PhysicsViewController: UIViewController {
var animator: UIDynamicAnimator!
override func viewDidLoad() {
super.viewDidLoad()
// 1. Create animator with reference view
animator = UIDynamicAnimator(referenceView: view)
// 2. Create falling objects
let redView = UIView(frame: CGRect(x: 100, y: 50, width: 60, height: 60))
redView.backgroundColor = .systemRed
redView.layer.cornerRadius = 8
view.addSubview(redView)
let blueView = UIView(frame: CGRect(x: 200, y: 30, width: 60, height: 60))
blueView.backgroundColor = .systemBlue
blueView.layer.cornerRadius = 8
view.addSubview(blueView)
// 3. Gravity
let gravity = UIGravityBehavior(items: [redView, blueView])
gravity.gravityDirection = CGVector(dx: 0, dy: 1)
gravity.magnitude = 0.8
animator.addBehavior(gravity)
// 4. Collisions
let collision = UICollisionBehavior(items: [redView, blueView])
collision.translatesReferenceBoundsIntoBoundary = true
collision.collisionDelegate = self
animator.addBehavior(collision)
// 5. Physical properties
let itemBehavior = UIDynamicItemBehavior(items: [redView, blueView])
itemBehavior.elasticity = 0.6
itemBehavior.friction = 0.3
itemBehavior.density = 1.0
animator.addBehavior(itemBehavior)
// 6. Snap behavior on tap
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tap)
}
@objc
func handleTap(_ gesture: UITapGestureRecognizer) {
let point = gesture.location(in: view)
// Snap behavior for each object
for subview in view.subviews where subview is UIView {
animator.removeAllBehaviors()
let snap = UISnapBehavior(item: subview, snapTo: point)
snap.damping = 0.5
animator.addBehavior(snap)
}
}
}
extension PhysicsViewController: UICollisionBehaviorDelegate {
func collisionBehavior(_ behavior: UICollisionBehavior,
beganContactFor item1: UIDynamicItem,
with item2: UIDynamicItem,
at point: CGPoint) {
print("Collision at: (point)")
}
}The example creates two Views with gravity, collisions and physical properties. UISnapBehavior on tap snaps objects to the touch point with 0.5 damping. UICollisionBehaviorDelegate tracks collision start for sound and visual effects.
UIKit Dynamics is used in iOS applications for non-game interface physics. Popular scenarios: card stack animation (UISnapBehavior for snapping to center), screen shake on error (UIDynamicItemBehavior + UIPushBehavior), "falling stars" effect in onboarding (gravity + collision + field behavior).
For performance, UIKit Dynamics recommends no more than 15-20 simultaneous objects with physics. Each object requires collision and force calculations every frame. If the limit is exceeded, use CALayer instead of UIView — Core Animation handles layers more efficiently.
Frequently Asked Questions
UIKit Dynamics models physical interactions (gravity, collisions, forces) based on rigid body simulation. Core Animation animates CALayer properties (position, opacity, transform) along a specified timing curve. Dynamics is suitable for interactive physics, Core Animation for predefined transitions.
There is no direct support for UIKit Dynamics in SwiftUI. For physics effects, use UIKitView + UIDynamicAnimator inside UIViewRepresentable, or custom animations via Animation.spring() and withAnimation() in SwiftUI.
Call animator.removeAllBehaviors() — this removes all active physical behaviors. The animator automatically pauses when all objects reach rest. For temporary pause, use the animator.isPaused property or remove individual behaviors.
Apple recommends no more than 15-20 objects with full physics (gravity + collisions + forces). For mass animations, use UIFieldBehavior — it calculates forces centrally, without pairwise collisions, reducing CPU load.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also