Drawing a custom shape
SwiftUI's drawing functionality permits more than just using the built-in shapes: creating a custom shape is just a matter of creating a Path
component with the various components and then wrapping it in a Shape
object.
In this recipe, we will work through the basics of custom shape creation, implementing a simple rhombus, which is a geometric shape with four equal straight sides that resembles a diamond.
To create a custom shape in SwiftUI, our class must conform to the Shape
protocol, whose only function, path
(in: CGRect
), provides a rectangle that represents the frame where the shape will be rendered as a parameter, and returns a Path
struct that is the description of the outline of the shape.
The Path
struct has several drawing primitives that permit us to move the point, add lines and arcs, and so on. This allows us to define the outline of the shape.
Using a Path
object resembles a bit of the old educative language, Logo, where you are...