0
0
iOS Swiftmobile~20 mins

Custom shapes and paths in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Custom Shapes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2: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()
}
AA circle centered at (100,100)
BA triangle with base on top and point down
CA square with top-left corner at (50,50)
DA line from (50,50) to (150,50)
Attempts:
2 left
πŸ’‘ Hint
The path moves to (50,50), then draws lines to (150,50) and (100,150), then closes the shape.
πŸ“ Syntax
intermediate
2: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.
APath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 50), cornerRadius: 10)
BPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 50), radius: 10)
CPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 50), corner: 10)
DPath(roundedRect: CGRect(x: 0, y: 0, width: 100, height: 50), cornerRadius: 10.0, style: .circular)
Attempts:
2 left
πŸ’‘ Hint
Check the exact parameter names for the roundedRect initializer.
❓ lifecycle
advanced
2: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?
AOnly once when the shape is first created
BWhen the user taps the shape
COnly when the app launches
DEvery time the view needs to redraw the shape, such as on size or state changes
Attempts:
2 left
πŸ’‘ Hint
Think about when the UI needs to update visuals.
πŸ”§ Debug
advanced
2: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
  }
}
AThe path coordinates are outside the view bounds
BThe path is missing a stroke color
CThe path only has a line segment and no closed shape, so fill does not show
DThe path variable is not initialized
Attempts:
2 left
πŸ’‘ Hint
Think about how fill works with open paths.
🧠 Conceptual
expert
2: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?
ADraws a smooth curved line from the current point to the 'to' point using the control point to shape the curve
BDraws a straight line ignoring the control point
CDraws a circle centered at the control point
DCreates a dashed line between the current point and the 'to' point
Attempts:
2 left
πŸ’‘ Hint
Think about how quadratic BΓ©zier curves work.