Custom shapes let you draw unique designs in your app. Paths help you create lines and curves to make these shapes.
Custom shapes and paths in iOS Swift
Introduction
Syntax
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))
path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY))
path.closeSubpath()
return path
}
}The Shape protocol requires a path(in: CGRect) method.
Use Path to draw lines and curves inside the given rectangle.
Examples
iOS Swift
struct Triangle: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.midX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
path.closeSubpath()
return path
}
}iOS Swift
struct CircleShape: Shape {
func path(in rect: CGRect) -> Path {
Path(ellipseIn: rect)
}
}iOS Swift
struct CustomPath: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.minX, y: rect.midY))
path.addQuadCurve(to: CGPoint(x: rect.maxX, y: rect.midY), control: CGPoint(x: rect.midX, y: rect.minY))
return path
}
}Sample App
This program draws a yellow star shape centered in a 200x200 frame with a shadow. It uses math to place points around a circle to form the star.
iOS Swift
import SwiftUI struct StarShape: Shape { func path(in rect: CGRect) -> Path { var path = Path() let center = CGPoint(x: rect.midX, y: rect.midY) let points = 5 let radius = min(rect.width, rect.height) / 2 let angle = Double.pi * 2 / Double(points * 2) for i in 0..<(points * 2) { let length = i.isMultiple(of: 2) ? radius : radius / 2 let x = center.x + CGFloat(cos(Double(i) * angle - Double.pi / 2)) * length let y = center.y + CGFloat(sin(Double(i) * angle - Double.pi / 2)) * length if i == 0 { path.move(to: CGPoint(x: x, y: y)) } else { path.addLine(to: CGPoint(x: x, y: y)) } } path.closeSubpath() return path } } struct ContentView: View { var body: some View { StarShape() .fill(Color.yellow) .frame(width: 200, height: 200) .shadow(radius: 5) } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Important Notes
Use move(to:) to start a new point in the path.
Use addLine(to:) to draw straight lines between points.
Use closeSubpath() to connect the last point back to the first.
Summary
Custom shapes let you create unique visuals by drawing paths.
The Shape protocol requires a path(in: CGRect) method.
Use Path methods like move(to:) and addLine(to:) to build your shape.
Practice
1. What is the main purpose of the
path(in: CGRect) method in a SwiftUI Shape?easy
Solution
Step 1: Understand the role of
This method is required by thepath(in: CGRect)Shapeprotocol to define the shape's outline.Step 2: Recognize what the method returns
It returns aPaththat describes the shape's drawing instructions inside the given rectangle.Final Answer:
To define the outline of the custom shape by drawing paths. -> Option DQuick Check:
path(in: CGRect)defines shape outline [OK]
Hint: Remember:
path(in:) draws the shape outline [OK]Common Mistakes:
- Confusing path method with color or animation methods
- Thinking it handles user interaction
- Assuming it sets layout constraints
2. Which of the following is the correct way to start drawing a path at a specific point in SwiftUI?
easy
Solution
Step 1: Recall the correct method to move the path cursor
ThePathtype usesmove(to:)to set the starting point.Step 2: Verify method names
Onlymove(to:)is a valid method; others do not exist in SwiftUI'sPath.Final Answer:
path.move(to: CGPoint(x: 50, y: 50)) -> Option BQuick Check:
Usemove(to:)to start path [OK]
Hint: Use
move(to:) to set path start point [OK]Common Mistakes:
- Using non-existent methods like start or begin
- Confusing with other graphics frameworks
- Forgetting to use CGPoint for coordinates
3. Given the following SwiftUI shape code, what shape will be drawn?
struct Triangle: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: rect.midX, y: rect.minY))
path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY))
path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
path.closeSubpath()
return path
}
}medium
Solution
Step 1: Analyze the points used in the path
The path starts at the top center (midX, minY), then draws lines to bottom left and bottom right corners.Step 2: Understand the shape formed
Connecting these points and closing the path forms a triangle pointing upwards.Final Answer:
A triangle pointing upwards -> Option AQuick Check:
Points form upward triangle [OK]
Hint: Visualize points: top center then bottom corners [OK]
Common Mistakes:
- Mixing up coordinate system orientation
- Assuming shape is a rectangle or circle
- Ignoring the order of points
4. Identify the error in this SwiftUI custom shape code:
struct LineShape: Shape {
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: rect.width, y: 0))
return path
}
}medium
Solution
Step 1: Check the path commands
The path moves to (0,0) and draws a line to (rect.width, 0), which is valid inside the rect.Step 2: Verify if closing the path is necessary
Closing a path is optional; lines can be open shapes without errors.Final Answer:
No error; the code correctly draws a horizontal line. -> Option AQuick Check:
Open paths are valid; no error here [OK]
Hint: Open paths don't require closing to be valid [OK]
Common Mistakes:
- Assuming closeSubpath() is mandatory
- Confusing rect.width with rect.maxX (both valid here)
- Thinking (0,0) is outside rect bounds
5. You want to create a custom SwiftUI shape that draws a star with 5 points. Which approach correctly combines path methods to achieve this?
hard
Solution
Step 1: Understand star shape construction
A star is made by connecting points with straight lines in a specific order.Step 2: Identify correct path methods
Starting withmove(to:), then connecting points withaddLine(to:), and closing the path withcloseSubpath()correctly draws the star.Step 3: Evaluate other options
Using ellipses or rectangles won't form a star shape; quad curves create smooth curves, not sharp star points.Final Answer:
Usemove(to:)to start at one point, then useaddLine(to:)to connect all star points in order, finally callcloseSubpath(). -> Option CQuick Check:
Star = move + addLine + closeSubpath [OK]
Hint: Connect star points with lines, then close path [OK]
Common Mistakes:
- Trying to use curves instead of lines for star points
- Using shapes like ellipses or rectangles incorrectly
- Forgetting to close the path to complete the shape
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
