A SwiftUI custom ProgressRing Shape with a stored progress property jumps to its new value even inside withAnimation; why, and how do you fix it?
answer
- Shape is already Animatable
- only one property is interpolated
- the fallback data is empty
- setter called every frame
- or trim a Circle
basics
~20 sShape conforms to Animatable, but without an animatableData implementation it falls back to EmptyAnimatableData, so SwiftUI has nothing to interpolate and draws the end state. Expose progress through animatableData, or build the ring from Circle().trim(from:to:).
solid answer
~40 sSwiftUI never animates a view object: under `withAnimation` or `animation(_:value:)` it reads the old and new `animatableData` of each `Animatable` value, interpolates with `VectorArithmetic`, and calls the setter on every frame before redrawing. `Shape` already conforms to `Animatable`, and a type that supplies no `animatableData` falls back to `EmptyAnimatableData`, so a stored `progress` is invisible to that process and the ring jumps. The fix is a computed `animatableData` with `get { progress }` and `set { progress = newValue }`, after which `path(in:)` draws every intermediate value. If the ring is just a stroked arc, `Circle().trim(from: 0, to: progress)` animates out of the box and is often the better review comment. If it still jumps, check that an animation reaches the change and that the ring keeps its identity.
code
swift · 35 linesimport SwiftUI
struct ProgressRing: Shape {
var progress: Double
var animatableData: Double {
get { progress }
set { progress = newValue }
}
func path(in rect: CGRect) -> Path {
var path = Path()
path.addArc(
center: CGPoint(x: rect.midX, y: rect.midY),
radius: min(rect.width, rect.height) / 2,
startAngle: .degrees(-90),
endAngle: .degrees(-90 + 360 * progress),
clockwise: false
)
return path
}
}
struct UploadRing: View {
@State private var progress = 0.2
var body: some View {
ProgressRing(progress: progress)
.stroke(.tint, style: StrokeStyle(lineWidth: 8, lineCap: .round))
.frame(width: 80, height: 80)
.onTapGesture {
withAnimation(.smooth) { progress = 0.9 }
}
}
}go deeper
Recall that a custom Shape animates only what its animatableData property exposes, and that Circle().trim(from:to:) already animates.
Explain the mechanism: old and new animatableData, VectorArithmetic interpolation, and the setter called each frame before path(in:) draws.
Diagnose the jump quickly, rule out a missing animation or a lost identity, and decide between a custom shape and trim, clamping overshoot where needed.
Set a team pattern for animated drawing components: when custom Animatable shapes are worth their maintenance over built-in shapes and modifiers.
## The symptom A team builds a `ProgressRing` that conforms to `Shape`, stores `var progress: Double`, and draws an arc from the top of the circle to `progress` of a full turn. Elsewhere on the same screen, `withAnimation(.smooth) { progress = 0.9 }` smoothly slides a label and fades a badge, but the ring **jumps** straight from 20% to 90%. Nothing is wrong with the trigger: the other views animate in the same transaction. The problem is what the ring tells SwiftUI it can animate. ## What SwiftUI actually animates SwiftUI does not move view objects. When state changes under an animation, it compares the old and new values of each **`Animatable`** piece of the view, and Apple's documentation describes the process precisely: SwiftUI "reads the old and new `animatableData` values, then interpolates between them over successive frames using `VectorArithmetic` operations. The framework calls the `animatableData` setter on each frame." Three facts follow: - **`Shape` already conforms to `Animatable`.** You never add the conformance yourself. - **Only `animatableData` is interpolated.** Stored properties are not inspected one by one. - **The fallback is empty.** A type that does not provide `animatableData` gets the default for `EmptyAnimatableData`, which Apple describes as suitable for types "that do not have any animatable properties." So the custom ring has nothing to interpolate. SwiftUI applies the new state in one step and `path(in:)` draws the final arc. The other views animate because their modifiers (offset, opacity) expose animatable data of their own. ## The fix Expose the value that should move: 1. Add a computed `animatableData` whose type is `VectorArithmetic` (`Double` here). 2. Return `progress` from the getter and assign `newValue` to `progress` in the setter. 3. Keep `path(in:)` a pure function of `progress`; SwiftUI calls the setter with each intermediate value and redraws, so the arc grows frame by frame. | Ring implementation | Animates? | Why | |---|---|---| | Custom `Shape`, no `animatableData` | No, it jumps | `EmptyAnimatableData` carries nothing | | Custom `Shape`, `animatableData` returns `progress` | Yes | The interpolated value reaches `path(in:)` | | `Circle().trim(from: 0, to: progress)` | Yes | The trimmed built-in shape is already animatable | | Custom `Shape` whose only value is an `Int` percent | Not smoothly | `Int` is not `VectorArithmetic`; converting back to `Int` in a setter steps in whole percents | In a review, the simplest answer is often the third row: a stroked `Circle` trimmed to `progress` and rotated to start at the top needs no custom shape at all. A custom shape earns its place when the geometry is something `trim` cannot express, such as a segmented ring or a gauge with a variable sweep. ## Writing the setter well The setter runs on every frame, which makes it the place for logic that must hold for intermediate values: - **Clamping.** A bouncy spring can overshoot; `progress = min(max(newValue, 0), 1)` keeps the arc within one turn. Apple's documentation names clamping and normalisation as reasons for a hand-written `animatableData`. - **Units.** Animate a `Double` fraction, not an `Int` percent you convert inside `path(in:)`. - **Several values.** Two moving values, such as a start and an end, go into an `AnimatablePair`. ## When the ring still jumps after the fix If `animatableData` is correct and the ring still jumps, the cause lies outside this shape: 1. **No animation reaches the change.** The state must change inside `withAnimation`, or the ring must carry `animation(_:value:)` watching `progress`. The value-less `animation(_:)`, deprecated since iOS 15, should not be the fix. 2. **The ring lost its identity.** A changing `id(_:)` or an `if`/`else` that swaps two branches inserts a new view instead of animating the old one, so you see a transition, not interpolation. The first is the topic of animation triggers and the second of view identity; here it is enough to rule them out quickly. ## The same mechanism elsewhere Any `ViewModifier` or `View` can conform to `Animatable`. A label that counts up from 0 to 1,250 cannot interpolate its `Text`, but a modifier with a `Double` `animatableData` receives each intermediate number and formats it. That is the general answer to "how do you animate a value SwiftUI cannot": give SwiftUI a vector it can interpolate, and turn each frame's value into the view yourself.
- How would you animate a number counting up in a label?Text cannot interpolate its string, so wrap the number in a `ViewModifier` that conforms to `Animatable` with a `Double` `animatableData`. SwiftUI sets the interpolated number each frame and the modifier's body formats it. `contentTransition(.numericText())` animates digit changes too, but it does not show the intermediate numbers.
- Why might you clamp inside the animatableData setter?A bouncy spring overshoots its target, so an interpolated progress can briefly exceed 1 or drop below 0 and draw more than a full turn. Clamping in the setter keeps every frame valid. Apple's documentation names clamping, normalisation and driving a derived value as reasons for a hand-written `animatableData`.
- The ring still jumps after adding animatableData; what do you check next?First that an animation reaches the change: the state must change inside `withAnimation`, or the ring needs `animation(_:value:)` watching `progress`. Then identity: a changing `id(_:)` or an if/else that swaps branches inserts a new ring instead of animating the old one, so you see a transition rather than interpolation.
- SwiftUI interpolates every stored property of a custom Shape on its own.
- The ring jumps because withAnimation does not reach custom shapes.
- Making progress a @State property inside the Shape makes it animate.
- An Int percentage works as animatableData if path(in:) converts it.
- SwiftUI morphs the old path into the new path automatically.