In SwiftUI, how do you make a like counter's Text roll its digits when the count changes, and why can contentTransition seem to do nothing?
answer
- content changes, not insertion
- a transition tailored to numbers
- only inside an animated change
- an environment value flowing down
basics
~10 sApply .contentTransition(.numericText(value:)) to the Text and change the count inside an animation; content transitions only take effect when the update carries an Animation, so a plain count += 1 shows no effect.
solid answer
~40 s`contentTransition(_:)` chooses how the **content inside one view** animates when it changes, as opposed to `transition(_:)`, which animates a view being inserted or removed. For a counter, `Text(count, format: .number).contentTransition(.numericText(value: Double(count)))` asks for a transition tailored to numeric text; iOS 16 has `numericText(countsDown:)` and iOS 17 added the `value:` form. The usual reason it seems to do nothing is that the change is not animated: Apple's documentation says content transitions only take effect within transactions that apply an `Animation`, so the increment must happen inside `withAnimation` or under `animation(_:value:)`. The modifier is applied through the environment, so it also reaches text views nested inside the view it is written on.
code
swift · 19 linesstruct LikeCounter: View {
@State private var isLiked = false
@State private var count = 41
var body: some View {
HStack {
Image(systemName: isLiked ? "heart.fill" : "heart")
Text(count, format: .number)
.contentTransition(.numericText(value: Double(count)))
}
.onTapGesture {
// Without an animation, the content transition has no effect.
withAnimation(.snappy) {
isLiked.toggle()
count += isLiked ? 1 : -1
}
}
}
}go deeper
Recall that contentTransition(.numericText()) on a Text changes how its number animates, and that the change must be animated.
Explain content versus view transitions, the iOS 16 countsDown form versus the iOS 17 value form, and that the modifier flows through the environment.
Diagnose a counter that does not roll: a missing animation, an override clearing it, or an id(_:) that turns the change into a replacement.
Be ready to decide whether numeric and symbol content transitions belong in a shared component so every counter and badge in the app behaves the same.
## Content transitions versus view transitions SwiftUI has two kinds of transition, and confusing them is the first mistake candidates make: - A **view transition**, set with `transition(_:)`, animates a view being **inserted into or removed from** the hierarchy, such as a toast sliding in. Those belong with insertion and hero effects. - A **content transition**, set with `contentTransition(_:)` (iOS 16), animates a change to the **content within a single view** that stays in place. Apple defines `ContentTransition` as "a kind of transition that applies to the content within a single view, rather than to the insertion or removal of a view." A like counter that goes from 41 to 42 is the second case: the `Text` keeps its identity and only its characters change. ## The available content transitions | Value | Documented behaviour | |---|---| | `.identity` | Content changes "shouldn't animate" | | `.opacity` | Content fades out and in | | `.interpolate` | Views "attempt to interpolate their contents", such as a text's font and colour | | `.numericText(countsDown:)` | For numeric `Text`; "in certain environments" enables a transition tailored to digits counting up or down (iOS 16) | | `.numericText(value:)` | For `Text` displaying numbers, taking the number itself (iOS 17) | | `.symbolEffect`, `.symbolEffect(_:options:)` | Symbol image replacement effects | For the counter, the iOS 17 form is the natural choice because it takes the displayed number instead of a direction flag: ```swift Text(count, format: .number) .contentTransition(.numericText(value: Double(count))) ``` ## Why it can appear to do nothing There are three common reasons, in the order to check them: 1. **No animation.** The documentation is explicit: content transitions "only take effect within transactions that apply an Animation to the views inside the contentTransition(_:) modifier." A plain `count += 1` in a tap handler has no animation, so the digits simply change. Wrap the change, `withAnimation(.snappy) { count += 1 }`, or give the `Text` an `animation(_:value:)` that watches `count`. 2. **An animation removed below.** A `transaction { $0.animation = nil }` or `animation(nil, value: count)` between the animated change and the text clears the animation, and the content transition has nothing to run with. 3. **A changed identity.** If the code gives the text `.id(count)` to force a refresh, every change replaces the view, so SwiftUI performs an insertion and removal instead of a content change and the numeric transition never applies. Also keep expectations at the documented strength: Apple says the numeric transition is enabled "in certain environments", so treat it as a request for the best available effect rather than a guaranteed digit roll everywhere. ## How the modifier reaches the text The SDK's inlined implementation of `contentTransition(_:)` is a single line: it sets the `contentTransition` **environment value**. Two consequences follow: - Writing it on a container such as an `HStack` applies it to the text views inside, because environment values flow down the hierarchy. - A nearer `contentTransition(_:)` on a child overrides it for that child, as with any environment value. The environment also has `contentTransitionAddsDrawingGroup`; setting it to `true` makes SwiftUI wrap content transitions in a `drawingGroup`, which controls whether they use GPU-accelerated rendering. ## Putting it together for a like button ```swift HStack { Image(systemName: isLiked ? "heart.fill" : "heart") Text(count, format: .number) .contentTransition(.numericText(value: Double(count))) } .onTapGesture { withAnimation(.snappy) { isLiked.toggle() count += isLiked ? 1 : -1 } } ``` The `withAnimation` call supplies the animation, the numeric content transition decides how the digits change, and the heart animates with the same spring. ## Availability - `contentTransition(_:)`, `ContentTransition` and `numericText(countsDown:)` arrived in iOS 16. - `numericText(value:)` arrived in iOS 17, the baseline for most production apps today. - An app that still deploys to iOS 16 can use the `countsDown:` form there, passing `true` when the new number is smaller than the old one, and switch to the `value:` form behind an `#available(iOS 17, *)` check. ## Pitfalls - Using `transition(_:)` on the `Text` and expecting the digits to roll; a transition needs an insertion or removal. - Forgetting the animation and concluding the modifier is broken. - Forcing identity changes with `id(_:)`, which turns a content change into a replacement. - Expecting the numeric effect on a view that is not a `Text`; Apple describes the numeric transitions as intended for `Text` views displaying numbers.
- What is the difference between contentTransition(.opacity) and transition(.opacity) on a Text?`contentTransition(.opacity)` fades between the old and new content of a text view that stays in the hierarchy, for example when its string changes. `transition(.opacity)` applies only when the text view itself is inserted or removed, for example inside an `if` whose condition changes under an animation. One animates content, the other animates presence.
- Why might contentTransition(.interpolate) suit a change of font size and colour on the same Text?Apple's documentation describes `interpolate` as making views attempt to interpolate their contents. Font size and colour both affect the paths of the glyphs, so the text can morph gradually between the two styles during the animation, whereas `opacity` would just fade from one rendering to the other.
- contentTransition works on its own, so a plain count += 1 shows the rolling digits.
- transition(_:) and contentTransition(_:) are two names for the same thing.
- Adding .id(count) to the Text helps the numeric transition run.
- numericText guarantees a digit roll in every context.