Bird
0
0

Which of the following SwiftUI code snippets correctly applies implicit animations to both properties using the .animation modifier?

hard📝 Application Q15 of 15
iOS Swift - Animations
You want to animate a view's size and color changes together smoothly when a toggle is switched. Which of the following SwiftUI code snippets correctly applies implicit animations to both properties using the .animation modifier?
AVStack { Rectangle() .frame(width: isOn ? 200 : 100, height: 100) .foregroundColor(isOn ? .green : .gray) .animation(.easeInOut, value: isOn) Toggle("Toggle", isOn: $isOn) }
BVStack { Rectangle() .frame(width: isOn ? 200 : 100, height: 100) .foregroundColor(isOn ? .green : .gray) Toggle("Toggle", isOn: $isOn) .animation(.easeInOut, value: isOn) }
CVStack { Rectangle() .frame(width: isOn ? 200 : 100, height: 100) .foregroundColor(isOn ? .green : .gray) Toggle("Toggle", isOn: $isOn) } .animation(.easeInOut, value: isOn)
DVStack { Rectangle() .frame(width: isOn ? 200 : 100, height: 100) .foregroundColor(isOn ? .green : .gray) } .animation(.easeInOut, value: isOn) Toggle("Toggle", isOn: $isOn)
Step-by-Step Solution
Solution:
  1. Step 1: Identify where to apply .animation for multiple property changes

    Applying .animation directly on the Rectangle view with value: isOn animates all its property changes smoothly.
  2. Step 2: Check the placement of Toggle and animation modifier

    VStack { Rectangle() .frame(width: isOn ? 200 : 100, height: 100) .foregroundColor(isOn ? .green : .gray) .animation(.easeInOut, value: isOn) Toggle("Toggle", isOn: $isOn) } applies .animation on Rectangle and keeps Toggle outside, which is correct. VStack { Rectangle() .frame(width: isOn ? 200 : 100, height: 100) .foregroundColor(isOn ? .green : .gray) Toggle("Toggle", isOn: $isOn) } .animation(.easeInOut, value: isOn) applies animation on VStack, which animates all children but may cause unwanted effects on Toggle.
  3. Final Answer:

    Snippet with .animation(.easeInOut, value: isOn) on Rectangle -> Option A
  4. Quick Check:

    Apply .animation on view changing properties for smooth combined animation [OK]
Quick Trick: Apply .animation on the view with changing properties [OK]
Common Mistakes:
  • Applying .animation on Toggle instead of changing view
  • Applying .animation on container causing unwanted animations
  • Placing .animation outside view hierarchy incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes