I'm trying to use my own UI for PencilKit tools (pens, eraser, undo, redo, etc), like in this sample code :
This works pretty well, I'm struggling in finding a way to manually set the PKEraserTool width property, as obviously it is paramount for the user to be able to freely set the eraser radius depending on his needs.
Code Block struct WriterCanvas: UIViewRepresentable { @Binding var canvasView: PKCanvasView func makeUIView(context: Context) -> PKCanvasView { self.canvasView.tool = PKInkingTool(.pen, color: .black, width: 1) return canvasView } func updateUIView(_ uiView: PKCanvasView, context: Context) { } } struct Writer: View { @Environment(\.undoManager) var undoManager @State private var canvasView = PKCanvasView() var body: some View { VStack() { HStack(spacing: 10) { Button("Clear") { self.canvasView.drawing = PKDrawing() } Button("Pen 1") { self.canvasView.tool = PKInkingTool( .pen, color: .black, width: 1 ) } Button("Pen 2") { self.canvasView.tool = PKInkingTool( .pen, color: .blue, width: 5 ) } Button("Pen 3") { self.canvasView.tool = PKInkingTool( .pen, color: UIColor( red:0.5, green:0.0, blue:0.0, alpha:0.2), width: 15 ) } Button("Eraser") { self.canvasView.tool = PKEraserTool( PKEraserTool.EraserType.bitmap ); } Button("Remover") { self.canvasView.tool = PKEraserTool( PKEraserTool.EraserType.vector ); } Button("Lasso") { self.canvasView.tool = PKLassoTool() } Button("Undo") { self.undoManager?.undo() } Button("Redo") { self.undoManager?.redo() } } WriterCanvas(canvasView: $canvasView) } } }
This works pretty well, I'm struggling in finding a way to manually set the PKEraserTool width property, as obviously it is paramount for the user to be able to freely set the eraser radius depending on his needs.