skip to content

In SwiftUI, how do you animate a state change with a spring, and how do the smooth, snappy and bouncy presets differ?

level: juniorimportance: should knowfreq 60%

answer

  1. curves versus physical motion
  2. two parameters: duration and bounce
  3. zero, a little, more overshoot
  4. velocity carried into the next spring

basics

~10 s

Pass a spring Animation such as .bouncy or .spring(duration: 0.4, bounce: 0.3) to withAnimation or animation(_:value:); .smooth has no bounce, .snappy a little and .bouncy the most, all with a 0.5-second default duration.

solid answer

~40 s

A spring is just another `Animation` value, so it goes wherever an animation goes: `withAnimation(.bouncy) { isLiked.toggle() }` or `.animation(.snappy, value: isLiked)`. The general form is `.spring(duration:bounce:blendDuration:)`, defaulting to a duration of 0.5 and a bounce of 0. The presets are named points on that scale: `.smooth` has no bounce, `.snappy` a small bounce and `.bouncy` the most; each also takes `duration:` and `extraBounce:` to tune it. Unlike `easeInOut` or `linear`, which follow a fixed curve, a spring is simulated motion, and Apple documents that a spring replacing another spring on the same property keeps its velocity, so repeated taps feel continuous instead of restarting.

code

swift · 15 lines
swift
struct LikeHeart: View {
    @State private var isLiked = false

    var body: some View {
        Image(systemName: isLiked ? "heart.fill" : "heart")
            .font(.largeTitle)
            .foregroundStyle(isLiked ? .red : .secondary)
            .scaleEffect(isLiked ? 1.2 : 1.0)
            .onTapGesture {
                withAnimation(.bouncy(duration: 0.4, extraBounce: 0.05)) {
                    isLiked.toggle()
                }
            }
    }
}

go deeper

for a junior

Recall that a spring is an Animation value passed to withAnimation or animation(_:value:), and the order smooth, snappy, bouncy from no overshoot to the most.

for a middle

Explain duration and bounce, the 0.5 and 0 defaults, extraBounce on the presets, and why a spring's duration is not a hard end time.

for a senior

Show when velocity preservation matters, such as rapid taps or a released drag, and how you tune presets instead of inventing a new curve per screen.

for a principal

Be ready to argue for a small shared set of spring presets in a design system and how a team keeps motion consistent across features.

## Animation curves and springs An **`Animation`** value in SwiftUI describes how a view moves from one rendered state to the next. There are two families: - **Timing curves.** `linear`, `easeIn`, `easeOut`, `easeInOut` (each also with a `duration:`) and `timingCurve` follow a fixed shape over a fixed time. Apple's documentation notes that a linear animation has "a mechanical feel" because its speed is constant, while easing varies acceleration for a more natural feel. - **Springs.** The motion is simulated like a mass on a spring, so it accelerates, can overshoot the target and settles. Springs suit interactive changes such as a button press or a drag release. Either kind is passed the same way, which is the junior-level point: a spring is not a separate API, it is an `Animation`. ```swift withAnimation(.bouncy) { isLiked.toggle() } Image(systemName: "heart.fill") .scaleEffect(isLiked ? 1.25 : 1) .animation(.spring(duration: 0.4, bounce: 0.3), value: isLiked) ``` ## The duration-and-bounce spring The current spring API, added with the iOS 17 SDK, takes two parameters that a designer can reason about: - **`duration`** sets how long the motion feels. In the SDK's inlined code it is passed to the spring as its **response**; it is not a hard stop. The `Spring` type exposes the separate `settlingDuration` for how long the motion takes to come to rest. - **`bounce`** sets the overshoot. `0` gives no overshoot; larger values overshoot and oscillate more. - **`blendDuration`** (default `0`) blends the duration of a new spring with the one it replaces. `Animation.spring` with no arguments uses these defaults: duration `0.5`, bounce `0`. The older `spring(response:dampingFraction:blendDuration:)` form still exists for existing code; the `Spring` structure converts between the two descriptions, and Apple's example shows `Spring(duration: 0.5, bounce: 0.3)` corresponding to a mass of 1, stiffness of about 157.9 and damping of about 17.6. ## The three presets The presets are inlined in the SDK, so their values can be read directly: | Preset | What it calls | Feel | |---|---|---| | `.smooth` | `spring(duration: 0.5, bounce: 0)` | Settles with no overshoot | | `.snappy` | `spring(duration: 0.5, bounce: 0.15)` | Quick with a small overshoot | | `.bouncy` | `spring(duration: 0.5, bounce: 0.3)` | Visible overshoot, playful | Each has a function form, `.smooth(duration:extraBounce:)`, `.snappy(duration:extraBounce:)` and `.bouncy(duration:extraBounce:)`, where `extraBounce` is added to the preset's own bounce. The same three exist on `Spring` for code that works with spring values directly. `interactiveSpring` is a short spring (default duration 0.15) intended for motion that tracks a gesture. ## Why springs feel right for repeated input Apple describes `spring` as "a persistent spring animation": when mixed with other `spring()` or `interactiveSpring()` animations on the same property, each is replaced by its successor, **preserving velocity** from one animation to the next. If a user taps a like button three times quickly, each new spring picks up the heart's current speed instead of starting from rest, so the motion stays continuous. ## Choosing a curve or a spring - **Springs** for direct manipulation and for anything the user may interrupt or repeat: a press, a toggle, a released drag, a like button. Because velocity carries over between springs, an interruption looks like a change of direction rather than a restart. - **Timing curves** for motion that should take a predictable time and is rarely interrupted, such as an indicator sweeping across a fixed duration or a fade that has to line up with another timed effect. - **`.smooth`** when you want a spring's continuity without visible overshoot; it suits layout changes such as an expanding card. - **`.snappy` or `.bouncy`** when the motion should feel responsive or playful, usually on small elements such as icons and badges. ## Shaping any animation All animations, springs included, accept modifiers: 1. `delay(_:)` to start later. 2. `speed(_:)` to play faster or slower. 3. `repeatCount(_:autoreverses:)` or `repeatForever(autoreverses:)` to repeat. ## Mistakes to avoid - Treating `duration` as the exact time the spring ends; the spring can keep settling after it. - Reaching for a large `bounce` on every element; interface springs usually want little or none, and `.smooth` is a sensible starting point. - Assuming a spring needs a special modifier; it is passed to `withAnimation` or `animation(_:value:)` like any curve. - Choosing durations by taste alone; motion design guidance about duration and easing belongs to the design system, and SwiftUI's job is to express it.

  • Does spring(duration: 0.5, bounce: 0.3) end exactly 0.5 seconds after it starts?
    Not necessarily. The SDK passes `duration` to the spring as its response, the perceived pace of the motion. A bouncy spring can overshoot and keep settling after that; the `Spring` type exposes `settlingDuration` separately. That is why chaining work after an animation should use a completion rather than a timer set to the duration.
  • What happens when a new spring starts on a property while the previous spring is still moving it?
    Apple documents `spring` as a persistent spring: when mixed with other `spring()` or `interactiveSpring()` animations on the same property, each is replaced by its successor and the velocity is preserved. The heart keeps moving at its current speed toward the new target, so repeated taps look continuous instead of jerking back to rest.

  • Springs need a special spring modifier instead of withAnimation or animation(_:value:).
  • A spring's duration is the exact moment the motion stops.
  • The .smooth preset bounces more than .bouncy.
  • A new spring always restarts from rest, ignoring the current velocity.
  • Linear is the natural choice for interface motion because its speed is even.