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 by implementing a simple rhombus, which is a geometric shape with four equal, straight sides that resembles a diamond.
Getting ready
Create a new single-view app with SwiftUI called RhombusApp
.
How to do it…
As we mentioned in the introduction, we are going to implement a Shape
object that defines the way our custom view must be visualized:
- Let's add a
Rhombus
struct:struct Rhombus: Shape { func path(in rect: CGRect) -> Path { Path() { path in path.move(to: CGPoint(x: rect.midX, ...