Bird
0
0

Given this code snippet, what will happen when the button is tapped?

medium📝 Predict Output Q5 of 15
iOS Swift - Animations
Given this code snippet, what will happen when the button is tapped?
@State private var show = false

Button("Toggle") {
  withAnimation(.spring()) {
    show.toggle()
  }
}

if show {
  Text("Hello")
}
AThe button tap does nothing.
BThe "Hello" text appears instantly without animation.
CThe "Hello" text appears with a spring animation.
DThe app throws a runtime error.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze button action with withAnimation

    The button toggles show inside withAnimation(.spring()), so changes animate with spring effect.
  2. Step 2: Understand conditional view rendering

    When show is true, the "Hello" text appears, animated by the spring animation.
  3. Final Answer:

    The "Hello" text appears with a spring animation. -> Option C
  4. Quick Check:

    withAnimation(.spring()) animates state-driven views [OK]
Quick Trick: Use .spring() for bouncy animations on state changes [OK]
Common Mistakes:
  • Expecting no animation
  • Thinking button does nothing
  • Assuming runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes