Challenge - 5 Problems
SwiftUI Animation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2: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()
}
}
}
}Attempts:
2 left
💡 Hint
Look at the initial color and the toggle inside withAnimation block.
✗ Incorrect
The circle starts blue because isRed is false. Tapping toggles isRed to true inside withAnimation, so the fill color animates smoothly to red.
❓ lifecycle
intermediate2: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
}
}
}
}Attempts:
2 left
💡 Hint
Check the modifier that triggers the animation.
✗ Incorrect
The animation is inside onAppear, so it runs once when the view first appears on screen.
🔧 Debug
advanced2: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()
}
}
}Attempts:
2 left
💡 Hint
Animations require explicit withAnimation or implicit animation modifiers.
✗ Incorrect
Without withAnimation, state changes happen instantly without animation. Wrapping isVisible.toggle() inside withAnimation fixes this.
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())
}
}
}Attempts:
2 left
💡 Hint
onAppear runs every time the view appears on screen.
✗ Incorrect
Each time DetailView appears, onAppear triggers the animation from width 100 to 200.
🧠 Conceptual
expert2:00remaining
Which statement about SwiftUI animated state changes is true?
Choose the correct statement about how SwiftUI handles animated state changes.
Attempts:
2 left
💡 Hint
Think about how withAnimation affects state changes.
✗ Incorrect
withAnimation tells SwiftUI to animate all view changes caused by the state change inside it, including layout and color.