Activity 1
Animate collection changes
Add and remove identified cards with a value-scoped animation and an insertion/removal transition. Verify stable IDs.
Hint: The data mutation and transition work together; identity determines which view changed.
SwiftUI Fundamentals
Use value-scoped animation, explicit transactions, transitions, and reduced-motion support to clarify interface changes.
Before you begin
Learning stage
Animation interpolates animatable values as the interface changes between states. withAnimation wraps a state mutation in an animation transaction. animation(_:value:) associates animation with changes to one specific value, avoiding the broad implicit-animation behavior that can animate unrelated updates.
A transition describes how a view enters or leaves the hierarchy. It only runs when identity or conditional structure actually inserts or removes that view. Changing opacity on an always-present view is animation, not insertion or removal.
Motion should communicate causality, hierarchy, continuity, or feedback. It should not delay common work. Read the accessibility Reduce Motion environment value and provide a quieter alternative when large movement is not essential.
Learning stage
The expandable section animates one state change and substitutes opacity for movement when Reduce Motion is enabled.
import SwiftUI
struct ExpandableLesson: View {
@State private var isExpanded = false
@Environment(.accessibilityReduceMotion) private var reduceMotion
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Button {
withAnimation(reduceMotion ? .easeOut(duration: 0.15) : .spring) {
isExpanded.toggle()
}
} label: {
Label("Key takeaways", systemImage: isExpanded ? "chevron.up" : "chevron.down")
}
if isExpanded {
Text("State drives the interface.")
.transition(reduceMotion ? .opacity : .move(edge: .top).combined(with: .opacity))
}
}
}
}Learning stage
Connect the SwiftUI concept to complete, maintainable application features.
Learning stage
Identify patterns that weaken ownership, identity, adaptation, accessibility, or user experience.
Learning stage
Build the behavior in a small Xcode project and review it in multiple states.
Activity 1
Add and remove identified cards with a value-scoped animation and an insertion/removal transition. Verify stable IDs.
Hint: The data mutation and transition work together; identity determines which view changed.
Activity 2
Build an expanding panel that uses movement normally and a short fade when Reduce Motion is enabled.
Hint: Preserve feedback and state clarity even when removing large motion.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
When a view is inserted into or removed from the hierarchy
A transition describes appearance and disappearance associated with structural identity changes.
QUESTION 2
It scopes the animation to changes in a specific value instead of potentially animating unrelated updates.
Explicit scope makes motion more predictable as a view grows.
Learning stage
Explain the design in terms of data flow, identity, lifecycle, platform behavior, and trade-offs.
Short answer: Animation interpolates changed values; a transition defines how a view enters or leaves the hierarchy.
A state mutation can animate properties such as scale, position, or color. If conditional structure inserts or removes a view, its transition supplies the insertion and removal effects, and an animation transaction drives them. Stable identity and structure determine which behavior applies.
Possible follow-ups
Learning stage
Effective SwiftUI motion explains change. Scope animations to meaningful values, use transitions for structural insertion and removal, and preserve comprehension for people who reduce motion.