SwiftUI SwiftUI Animations and Transitions 1 — Questions and Answers
Question 1: What function do you call to animate a state change in SwiftUI?
- animate()
- withAnimation() (Correct answer)
- Animation.run()
- transition()
Correct answer: withAnimation()
Wrapping a state change in `withAnimation {}` causes SwiftUI to animate any resulting view changes.
Question 2: What does the `.animation(_:value:)` modifier do in SwiftUI?
- Applies an animation to all state changes
- Applies an animation only when the specified value changes (Correct answer)
- Adds a looping animation
- Applies a transition animation
Correct answer: Applies an animation only when the specified value changes
`.animation(_:value:)` scopes the animation to trigger only when the given value changes, preventing unintended animations.
Question 3: Which animation creates a spring effect in SwiftUI?
- .linear
- .easeInOut
- .spring() (Correct answer)
- .interpolatingSpring()
Correct answer: .spring()
`.spring()` applies a spring-based animation with configurable response, dampingFraction, and blendDuration.
Question 4: What does the `.transition()` modifier control in SwiftUI?
- How a view animates continuously
- How a view enters and exits the view hierarchy (Correct answer)
- The animation curve applied to a view
- The sequence of animations
Correct answer: How a view enters and exits the view hierarchy
`.transition()` defines the animation applied when a view is inserted into or removed from the view hierarchy.
Question 5: Which built-in transition slides a view in from the leading edge?
- .slide
- .move(edge: .leading) (Correct answer)
- .push(from: .leading)
- .offset()
Correct answer: .move(edge: .leading)
`.move(edge: .leading)` slides a view in from or out to the leading edge of the screen.
Question 6: How do you create a repeating animation in SwiftUI?
- .animation(.linear.loop())
- .animation(.linear.repeatForever()) (Correct answer)
- .repeatAnimation()
- .loop(animation:)
Correct answer: .animation(.linear.repeatForever())
Chaining `.repeatForever(autoreverses:)` onto an animation makes it repeat indefinitely.
What function do you call to animate a state change in SwiftUI?