Predict the output
The user taps Toggle. What animates?
@State private var expanded = false
VStack {
Rectangle()
.frame(height: expanded ? 200 : 50)
.animation(.spring, value: expanded)
Text(expanded ? "Less" : "More")
.font(expanded ? .title : .body)
Button("Toggle") { expanded.toggle() }
}Hint 1 of 1
Which view has the animation modifier?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: B Only the rectangle's height
Correct. The modifier animates changes in the view it is attached to.
Why not the others?
- A Both the rectangle and the text. That needs
withAnimation { expanded.toggle() }. - C Nothing, because the animation isn't inside withAnimation. The value-based modifier animates on its own when
expandedchanges. - D Only the text. The animation modifier sits on the rectangle.