SwiftUI SwiftUI Animations and Transitions 2 — Questions and Answers
Question 1: What is the `@Namespace` property wrapper used for in SwiftUI?
- Scoping state variables
- Defining matched geometry namespaces for hero animations (Correct answer)
- Creating view namespaces to avoid name collisions
- Storing animation references
Correct answer: Defining matched geometry namespaces for hero animations
`@Namespace` creates an animation namespace used with `.matchedGeometryEffect()` to animate shared geometry between views.
Question 2: What does `.matchedGeometryEffect(id:in:)` do in SwiftUI?
- Matches views by their frame sizes
- Synchronizes the geometry of two views across a transition for hero animations (Correct answer)
- Aligns views to the same grid
- Copies a view's frame to another
Correct answer: Synchronizes the geometry of two views across a transition for hero animations
`.matchedGeometryEffect` links two views by the same ID so SwiftUI interpolates their position and size during transitions.
Question 3: Which modifier applies an animation phase in the new Keyframe Animator (iOS 17)?
- .keyframeAnimator(initialValue:keyframes:content:) (Correct answer)
- .phaseAnimator()
- .animationPhase()
- .sequenceAnimation()
Correct answer: .keyframeAnimator(initialValue:keyframes:content:)
`KeyframeAnimator` (iOS 17) lets you define multi-property keyframe tracks to animate complex, choreographed sequences.
Question 4: What does `.phaseAnimator(_:content:animation:)` do in SwiftUI (iOS 17)?
- Runs an animation in a separate thread
- Cycles through a sequence of phases, animating between them (Correct answer)
- Applies physics simulation
- Syncs animation to audio phases
Correct answer: Cycles through a sequence of phases, animating between them
`PhaseAnimator` cycles through a sequence of states, automatically animating between each phase in a loop.
Question 5: How do you add a custom transition using `AnyTransition` in SwiftUI?
- Implement the Transition protocol
- Use AnyTransition.modifier(active:identity:) (Correct answer)
- Subclass Animation
- Use withAnimation and custom offsets only
Correct answer: Use AnyTransition.modifier(active:identity:)
`AnyTransition.modifier(active:identity:)` lets you define a custom transition using ViewModifier states for the active and identity states.
Question 6: What does the `delay` parameter on an animation do in SwiftUI?
- Pauses the app for that duration
- Delays the start of the animation by the given time interval (Correct answer)
- Slows the animation playback rate
- Schedules a view update
Correct answer: Delays the start of the animation by the given time interval
Calling `.delay()` on an animation causes it to wait the specified number of seconds before starting.
What is the `@Namespace` property wrapper used for in SwiftUI?