0
0
iOS Swiftmobile~20 mins

Canvas for drawing in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Canvas Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
}
AA red diagonal line from top-left (10,10) to bottom-right (100,100) with thickness 5
BNothing is drawn because the path is incomplete
CA red rectangle from (10,10) to (100,100)
DA blue diagonal line from top-left (10,10) to bottom-right (100,100) with thickness 5
Attempts:
2 left
💡 Hint
Look at the stroke color and the points used in the path.
lifecycle
intermediate
1: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?
AOnly when the @State variable changes
BOnly when the app restarts
CContinuously every frame regardless of state
DOnly when the user taps the Canvas
Attempts:
2 left
💡 Hint
Think about SwiftUI's reactive update system.
📝 Syntax
advanced
2: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"))
}
ASyntax error: Missing closing brace
BRuntime error: Invalid rectangle size
CNo error, draws a blue ellipse
DType error: Cannot convert String to Color
Attempts:
2 left
💡 Hint
Check the argument passed to .color()
navigation
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?
AAdd a timer to redraw Canvas every second
BReload the entire app to refresh the Canvas
CUse @ObservedObject or @StateObject to share data and trigger redraw
DUse a global variable without property wrappers
Attempts:
2 left
💡 Hint
Think about SwiftUI data flow and state management.
🧠 Conceptual
expert
3: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?
AShape automatically animates without extra code
BCanvas allows direct access to Core Graphics context for custom drawing commands
CCanvas is simpler and requires less code than Shape
DShape supports bitmap images but Canvas does not
Attempts:
2 left
💡 Hint
Think about flexibility and low-level drawing control.