Bird
0
0

What will be the visible effect when this SwiftUI code runs?

medium📝 Predict Output Q4 of 15
iOS Swift - Animations
What will be the visible effect when this SwiftUI code runs?
withAnimation {
  self.isExpanded.toggle()
}
Assuming isExpanded controls the height of a view.
AThe view's height changes instantly without animation.
BThe view's height changes smoothly with animation.
CThe view does not change height at all.
DThe app crashes due to missing animation parameters.
Step-by-Step Solution
Solution:
  1. Step 1: Understand withAnimation effect on state change

    Using withAnimation around a toggle animates the resulting view changes smoothly.
  2. Step 2: Relate isExpanded to view height

    Since isExpanded controls height, toggling it inside withAnimation animates the height change.
  3. Final Answer:

    The view's height changes smoothly with animation. -> Option B
  4. Quick Check:

    withAnimation toggles state with smooth UI change [OK]
Quick Trick: State changes inside withAnimation animate UI updates [OK]
Common Mistakes:
  • Expecting instant change
  • Thinking animation needs extra code
  • Assuming crash without parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes