Challenge - 5 Problems
Canvas Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you add this code to a SwiftUI Canvas?
Consider this SwiftUI Canvas code snippet. What will the user see rendered on the screen?
iOS Swift
Canvas { context, size in
context.stroke(Path { path in
path.move(to: CGPoint(x: 10, y: 10))
path.addLine(to: CGPoint(x: 100, y: 100))
}, with: .color(.red), lineWidth: 5)
}Attempts:
2 left
💡 Hint
Look at the stroke color and the points used in the path.
✗ Incorrect
The code draws a red line from point (10,10) to (100,100) with a line width of 5. The color is explicitly set to red, so option A is correct.
❓ lifecycle
intermediate1:30remaining
When does the Canvas redraw in SwiftUI?
Given a SwiftUI Canvas view that depends on a @State variable, when will the Canvas redraw its content?
Attempts:
2 left
💡 Hint
Think about SwiftUI's reactive update system.
✗ Incorrect
SwiftUI redraws views like Canvas when their dependent state changes. So if the Canvas depends on a @State variable, it redraws when that variable changes.
📝 Syntax
advanced2:00remaining
What error does this Canvas code produce?
Analyze this SwiftUI Canvas code snippet. What error will it cause?
iOS Swift
Canvas { context, size in
context.fill(Path(ellipseIn: CGRect(x: 50, y: 50, width: 100, height: 100)), with: .color("blue"))
}Attempts:
2 left
💡 Hint
Check the argument passed to .color()
✗ Incorrect
The .color() modifier expects a Color type, not a String. Passing "blue" as a string causes a type error.
advanced
2:30remaining
How to update Canvas drawing after navigation back?
You have a Canvas drawing view that depends on data updated in a second view. After navigating back, the Canvas does not update. What is the best way to fix this?
Attempts:
2 left
💡 Hint
Think about SwiftUI data flow and state management.
✗ Incorrect
Using @ObservedObject or @StateObject allows the Canvas view to observe changes in shared data and redraw automatically when data updates after navigation.
🧠 Conceptual
expert3:00remaining
Why use Canvas over Shape in SwiftUI for complex drawings?
Which reason best explains why you would choose Canvas instead of Shape for drawing complex graphics in SwiftUI?
Attempts:
2 left
💡 Hint
Think about flexibility and low-level drawing control.
✗ Incorrect
Canvas provides a drawing context giving low-level control over rendering, enabling complex custom graphics beyond what Shape's path-based drawing offers.