Drawing using the Canvas API
One of the most powerful features of UIKit is the possibility of creating a subclass of UIView where we can draw directly into the graphic context using Core Graphic Framework's functions.
SwiftUI implemented this functionality similarly via the Canvas View, where we can draw directly in the Core Graphic's context.
In this recipe, will explore the Canvas View by implementing a simple app to draw using our finger.
Getting ready
Let's implement a SwiftUI app called Drawing.
How to do it…
The app will use a struct as a model to save the touches. Then, each model will be used to draw a line in a Canvas View. To do this, follow these steps:
- First, create the
Line structmodel and add a@Stateproperty to the main View:struct Line { var points: [CGPoint] } struct ContentView: View { @State var lines: [Line] = [] var...