Challenge - 5 Problems
Custom Shapes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β ui_behavior
intermediate2:00remaining
What shape does this SwiftUI Path draw?
Look at the SwiftUI Path code below. What shape will appear on the screen?
iOS Swift
Path { path in
path.move(to: CGPoint(x: 50, y: 50))
path.addLine(to: CGPoint(x: 150, y: 50))
path.addLine(to: CGPoint(x: 100, y: 150))
path.closeSubpath()
}Attempts:
2 left
π‘ Hint
The path moves to (50,50), then draws lines to (150,50) and (100,150), then closes the shape.
β Incorrect
The code draws a triangle by connecting three points: (50,50), (150,50), and (100,150). Closing the path connects the last point back to the first.
π Syntax
intermediate2:00remaining
Which option correctly creates a rounded rectangle path in SwiftUI?
Choose the SwiftUI code snippet that correctly creates a rounded rectangle path with corner radius 10.
Attempts:
2 left
π‘ Hint
Check the exact parameter names for the roundedRect initializer.
β Incorrect
The correct initializer uses 'cornerRadius' as the parameter name for the radius of corners.
β lifecycle
advanced2:00remaining
When is the SwiftUI Shape's path(in:) method called?
In SwiftUI, the Shape protocol requires implementing path(in rect: CGRect). When does SwiftUI call this method?
Attempts:
2 left
π‘ Hint
Think about when the UI needs to update visuals.
β Incorrect
SwiftUI calls path(in:) whenever it needs to render or re-render the shape, including when the view size changes or state updates affect the shape.
π§ Debug
advanced2:00remaining
Why does this custom path not render anything?
This SwiftUI code defines a custom shape but nothing appears on screen. What is the likely cause?
struct MyShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.minX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
// Missing path.closeSubpath()
return path
}
}
iOS Swift
struct MyShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.minX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
return path
}
}Attempts:
2 left
π‘ Hint
Think about how fill works with open paths.
β Incorrect
Filling an open path with only a line segment results in no visible fill. Closing the path or stroking it is needed to see the shape.
π§ Conceptual
expert2:00remaining
What is the effect of using addQuadCurve(to:control:) in a SwiftUI Path?
In SwiftUI, what does the addQuadCurve(to:control:) method do when building a Path?
Attempts:
2 left
π‘ Hint
Think about how quadratic BΓ©zier curves work.
β Incorrect
addQuadCurve(to:control:) adds a quadratic BΓ©zier curve segment to the path, using the control point to define the curve's shape between the current point and the destination.