Component - Custom shapes and paths
This UI component draws a custom shape using paths. It shows how to create unique visuals by defining points and curves instead of using standard shapes like rectangles or circles.
This UI component draws a custom shape using paths. It shows how to create unique visuals by defining points and curves instead of using standard shapes like rectangles or circles.
UIView
└── CustomShapeView
└── CAShapeLayerpath(in: CGRect) method in a SwiftUI Shape?path(in: CGRect)Shape protocol to define the shape's outline.Path that describes the shape's drawing instructions inside the given rectangle.path(in: CGRect) defines shape outline [OK]path(in:) draws the shape outline [OK]Path type uses move(to:) to set the starting point.move(to:) is a valid method; others do not exist in SwiftUI's Path.move(to:) to start path [OK]move(to:) to set path start point [OK]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
}
}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
}
}move(to:), then connecting points with addLine(to:), and closing the path with closeSubpath() correctly draws the star.move(to:) to start at one point, then use addLine(to:) to connect all star points in order, finally call closeSubpath(). -> Option CPass the test and this pattern moves unknown