UIKit Dynamics — The Physics of Interaction in iOS

Author: IT Sectr Published: 2026-03-01 Reading time: 8 min

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

  • UIDynamicAnimator — the physics animation engine that links behaviors and objects
  • UIGravityBehavior — gravity simulation with a direction vector and acceleration magnitude
  • UICollisionBehavior — handles collisions between Views and reference view boundaries
  • UIDynamicItemBehavior — configures physical properties: elasticity, density, friction
  • UIAttachmentBehavior — connection between two points with spring or cable effect

What is UIKit Dynamics?

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 — The Physics Engine

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.

UIDynamicItem — Protocol for Animation

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.

Types of Dynamic Behaviors

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.

BehaviorPhysical EffectKey Parameters
UIGravityBehaviorGravity (force vector)gravityDirection (dx, dy), magnitude
UICollisionBehaviorObject and boundary collisionscollisionMode, translucence, collisionDelegate
UIAttachmentBehaviorConnection (spring/cable)anchorPoint, length, damping, frequency
UISnapBehaviorSnap to point with dampingsnapPoint, damping (0-1)
UIPushBehaviorPush/impulse (continuous or instantaneous)pushDirection, magnitude, mode
UIDynamicItemBehaviorObject physical propertieselasticity, friction, density, resistance
UIFieldBehaviorForce fields (vortex, magnetic, radial)position, strength, falloff, region

Combining Behaviors

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 Examples

Swift code for UIKit Dynamics creates a UIDynamicAnimator and adds behaviors. The example below demonstrates gravity with multiple objects, collisions and custom physical properties.

swift
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.

Practical Use Cases

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

How is UIKit Dynamics different from Core Animation?

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.

Can UIKit Dynamics be used in SwiftUI?

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.

How to stop UIKit Dynamics?

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.

How many objects can be animated simultaneously?

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

  • UIKit Dynamics — iOS physical animation framework with gravity, collision and force simulation
  • UIDynamicAnimator — the engine that manages behaviors and calculates physics every frame
  • 7 behavior types — UIGravityBehavior, UICollisionBehavior, UIAttachmentBehavior, UISnapBehavior, UIPushBehavior, UIDynamicItemBehavior, UIFieldBehavior
  • Combination — gravity + collision + itemBehavior creates realistic falling and bouncing physics
  • UIDynamicItem — protocol for physics participant objects (UIView implements by default)
  • Limitation — no more than 20 objects with full physics, not suitable for games and complex scenes
  • SwiftUI — use UIViewRepresentable to integrate UIKit Dynamics

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.

Discuss the project

Read also