2

Using PencilKit for iOS, how do I set the eraser tool to .bitmap for PKToolPicker?

I can't find any setting for PKToolPicker. Trying to use PKCanvasView to observe and set the tool's eraserType as .bitmap also does not work.

override func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
    var tool = toolPicker.selectedTool as? PKEraserTool

    if tool != nil {
        tool?.eraserType = .bitmap
    }
}
byaruhaf
  • 4,128
  • 2
  • 32
  • 50
Lim Thye Chean
  • 8,704
  • 9
  • 49
  • 88

2 Answers2

0

The PKEraser is a struct, so when you change its eraserType, you're actually modifying a copy of the tool that's being used in the canvas.

What you need to do is simply set the PKCanvasView tool property and it will work.

override func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
    var tool = toolPicker.selectedTool as? PKEraserTool

    if tool != nil {
        tool?.eraserType = .bitmap
    }

    // this line below will do the trick
    canvasView.tool = tool
}

Let me know if it worked!

  • Hi, thanks for the reply. I get: Value of type 'PKEraserTool?' does not conform to 'PKTool' in assignment for that additional statement. – Lim Thye Chean Jun 20 '20 at 05:53
0

Applies to iOS 13 and iOS 14

To set the toolpicker's selected tool as a bitmap eraser (where toolPicker is the PKToolPicker):

toolPicker?.selectedTool = PKEraserTool(.bitmap)

To set the canvas view's tool to a bitmap eraser (where canvasView is the PKCanvasView):

canvasView.tool = PKEraserTool(.bitmap)

This code, based on your example, will keep the toolpicker's erase tool as bitmap(pixel eraser) even if vector(object eraser) was chosen. (tested on iOS 14)

func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {

    if toolPicker.selectedTool is PKEraserTool {
        toolPicker.selectedTool = PKEraserTool(.bitmap)
    }
}
Marcy
  • 4,611
  • 2
  • 34
  • 52