0
0
iOS Swiftmobile~20 mins

Implicit animations (.animation modifier) in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Implicit Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you tap the button?

Consider this SwiftUI code snippet. What will you see when you tap the "Change Color" button?

iOS Swift
struct ContentView: View {
  @State private var isRed = false
  var body: some View {
    VStack {
      Rectangle()
        .fill(isRed ? Color.red : Color.blue)
        .frame(width: 100, height: 100)
        .animation(.easeInOut, value: isRed)
      Button("Change Color") {
        isRed.toggle()
      }
    }
  }
}
AThe rectangle instantly changes color without animation when tapped.
BThe rectangle smoothly changes color between red and blue when tapped.
CThe rectangle fades out completely when tapped.
DThe button does nothing when tapped.
Attempts:
2 left
💡 Hint

Look at the .animation modifier and the value parameter.

lifecycle
intermediate
2:00remaining
Why does the animation not run?

Why does this SwiftUI view NOT animate the color change when the button is tapped?

iOS Swift
struct ContentView: View {
  @State private var isRed = false
  var body: some View {
    VStack {
      Rectangle()
        .fill(isRed ? Color.red : Color.blue)
        .frame(width: 100, height: 100)
      Button("Change Color") {
        withAnimation {
          isRed.toggle()
        }
      }
    }
  }
}
ABecause the .animation modifier is missing on the Rectangle view.
BBecause the withAnimation block is incorrectly placed inside the Button action.
CBecause the Rectangle view does not have a frame modifier.
DBecause the isRed state variable is not marked with @State.
Attempts:
2 left
💡 Hint

Think about where the animation modifier should be to animate view changes.

📝 Syntax
advanced
2:00remaining
Which code compiles and animates the scale change?

Choose the correct SwiftUI code snippet that animates a circle scaling up and down when tapped.

A
Circle()
  .scaleEffect(isScaled ? 2 : 1)
  .animation(.spring(), value: isScaled)
  .onTapGesture { isScaled.toggle() }
B
Circle()
  .scaleEffect(isScaled ? 2 : 1)
  .animation(.spring())
  .onTapGesture { isScaled.toggle() }
C
Circle()
  .scaleEffect(isScaled ? 2 : 1)
  .animation(.spring(), value: isScaled)
  .onTapGesture { withAnimation { isScaled.toggle() } }
D
Circle()
  .scaleEffect(isScaled ? 2 : 1)
  .onTapGesture { isScaled.toggle() }
Attempts:
2 left
💡 Hint

Check the use of the .animation modifier with the value parameter.

🔧 Debug
advanced
2:00remaining
Why does the animation jump instead of animate smoothly?

Given this code, why does the rectangle jump to the new position instead of animating smoothly?

iOS Swift
struct ContentView: View {
  @State private var offset = CGSize.zero
  var body: some View {
    Rectangle()
      .frame(width: 100, height: 100)
      .offset(offset)
      .animation(.easeInOut, value: offset)
      .gesture(
        DragGesture()
          .onChanged { gesture in
            offset = gesture.translation
          }
          .onEnded { _ in
            offset = .zero
          }
      )
  }
}
ABecause the DragGesture is missing a call to withAnimation inside onEnded.
BBecause the .animation modifier is applied after the .offset modifier.
CBecause the offset state changes too frequently during dragging, animation cannot keep up.
DBecause the offset variable is not marked with @State.
Attempts:
2 left
💡 Hint

Think about how often the offset state changes during dragging.

🧠 Conceptual
expert
2:00remaining
What is the role of the 'value' parameter in .animation modifier?

In SwiftUI, what does the value parameter in the .animation(_:value:) modifier control?

AIt disables animation when the value is nil.
BIt sets the duration of the animation in seconds.
CIt specifies the delay before the animation starts.
DIt tells SwiftUI to animate changes only when the specified value changes.
Attempts:
2 left
💡 Hint

Think about when SwiftUI triggers the animation.