I'm trying to stack two PKCanvasView to use the lower layer for highlighing and the upper layer for pen writing, so that the highlighting strokes are below and not over the handwritten text, to keep it perfectly readable even after it has been highlighted.
For this I need to be able to redirect the Apple Pencil gestures to the appropriate layer(s), depending on the chosen tool : lower layer for the highlighter tool, upper layer for the pen tool, both layers for the eraser tool, etc.
I've easily managed to create my own tool buttons (Pen, Highlighter, Eraser, Selection, Undo, Redo) with SwiftUI and select the tool for a PKCanvasView, like in the example code below :
self.canvasView.tool = PKInkingTool(.pen, color: .black, width: 15)
But for the life of me, I still can't find a way to : intercept and redirect the gestures produced by the Apple Pencil device to the actice tool of one or several PKCanvasView, and/or get the gestures directly and emulate the tools directly on the PKCanvas or its stroke array;
manually set the some of the tools parameters when/after creating them (like the eraser tool width for instance);
apply a custom semi-transparent opacity to a PKCanvasView layer.
I'm becoming quite desperate at the moment, so any help about this would be highly appreciated !
Post
Replies
Boosts
Views
Activity
I'm trying to use my own UI for PencilKit tools (pens, eraser, undo, redo, etc), like in this sample code :
		@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.