0
0
iOS Swiftmobile~20 mins

Animated state changes in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SwiftUI Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this SwiftUI animation code?
Consider this SwiftUI view that toggles a circle's color with animation when tapped. What color will the circle show after tapping once?
iOS Swift
struct ContentView: View {
  @State private var isRed = false
  var body: some View {
    Circle()
      .fill(isRed ? Color.red : Color.blue)
      .frame(width: 100, height: 100)
      .onTapGesture {
        withAnimation {
          isRed.toggle()
        }
      }
  }
}
AThe circle changes from blue to red smoothly with animation.
BThe circle instantly changes from blue to red without animation.
CThe circle changes from red to blue smoothly with animation.
DThe circle does not change color on tap.
Attempts:
2 left
💡 Hint
Look at the initial color and the toggle inside withAnimation block.
lifecycle
intermediate
2:00remaining
Which SwiftUI lifecycle event triggers the animation in this code?
Given this SwiftUI view, when does the animation run?
iOS Swift
struct ContentView: View {
  @State private var scale: CGFloat = 1.0
  var body: some View {
    Circle()
      .scaleEffect(scale)
      .onAppear {
        withAnimation(.easeInOut(duration: 1)) {
          scale = 2.0
        }
      }
  }
}
AWhen the circle is tapped.
BWhen the view appears on screen.
CWhen the scale variable is manually changed outside the view.
DWhen the view disappears from screen.
Attempts:
2 left
💡 Hint
Check the modifier that triggers the animation.
🔧 Debug
advanced
2:00remaining
Why does this SwiftUI animation not run as expected?
This code intends to animate the opacity change, but the circle instantly disappears without animation. Why?
iOS Swift
struct ContentView: View {
  @State private var isVisible = true
  var body: some View {
    Circle()
      .opacity(isVisible ? 1 : 0)
      .onTapGesture {
        isVisible.toggle()
      }
  }
}
ABecause the Circle view does not support opacity modifier.
BBecause opacity cannot be animated in SwiftUI.
CBecause onTapGesture does not trigger state changes.
DBecause the opacity change is not wrapped inside withAnimation block.
Attempts:
2 left
💡 Hint
Animations require explicit withAnimation or implicit animation modifiers.
navigation
advanced
2:00remaining
What happens to the animation when navigating back in this SwiftUI NavigationStack?
This view animates a rectangle's width on appear. What happens when the user navigates back to this view?
iOS Swift
struct DetailView: View {
  @State private var width: CGFloat = 100
  var body: some View {
    Rectangle()
      .frame(width: width, height: 50)
      .onAppear {
        width = 100
        withAnimation(.linear(duration: 1)) {
          width = 200
        }
      }
  }
}

struct ContentView: View {
  var body: some View {
    NavigationStack {
      NavigationLink("Go to Detail", destination: DetailView())
    }
  }
}
AThe rectangle animates from width 100 to 200 every time the DetailView appears.
BThe rectangle animates only the first time DetailView appears, not on back navigation.
CThe rectangle width stays at 200 without animation on back navigation.
DThe rectangle does not animate at all.
Attempts:
2 left
💡 Hint
onAppear runs every time the view appears on screen.
🧠 Conceptual
expert
2:00remaining
Which statement about SwiftUI animated state changes is true?
Choose the correct statement about how SwiftUI handles animated state changes.
AAnimations in SwiftUI require manual frame-by-frame updates for smooth transitions.
BSwiftUI animations only work on color and opacity properties, not on layout changes.
CState changes inside withAnimation blocks animate all affected view properties automatically.
DState changes outside withAnimation blocks never update the UI.
Attempts:
2 left
💡 Hint
Think about how withAnimation affects state changes.