Bird
0
0

You want to animate a button's background color change when tapped. Which approach correctly combines state and animation in SwiftUI?

hard📝 Application Q8 of 15
iOS Swift - Animations
You want to animate a button's background color change when tapped. Which approach correctly combines state and animation in SwiftUI?
AUse a local variable for color and call withAnimation outside the button action.
BChange color directly without state and rely on .animation modifier without value.
CUse @State for color, change it inside withAnimation, and apply .animation with value on the button.
DUse @Binding for color and animate changes without withAnimation.
Step-by-Step Solution
Solution:
  1. Step 1: Use @State to hold color

    State variables trigger UI updates and animations when changed.
  2. Step 2: Change color inside withAnimation and apply .animation with value

    This ensures smooth animated transitions on the button's background color.
  3. Final Answer:

    Use @State for color, change it inside withAnimation, and apply .animation with value on the button. -> Option C
  4. Quick Check:

    State + withAnimation + .animation(value:) = smooth color change [OK]
Quick Trick: Combine @State, withAnimation, and .animation(value:) for color changes [OK]
Common Mistakes:
  • Using local variables instead of @State
  • Omitting withAnimation block
  • Using .animation without value parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes