1

I'm building a drawing app using PencilKit and I have a pre-loaded PKDrawing that loads with PKCanvasView. My goal it's to allow the user to play with the canvas using the pre-loaded PKDrawing as a reference. My question is: Is it possible to make the pre-loaded PKDraw unerasable? Please notice that I don't want to completely block the Eraser tool, users must be able to erase its own drawing, but not the reference one.screenshot

  • Two options I can think of: 1) Don't use a PKDrawing for the reference -- use an image 2) Use a PKDrawing on a separate canvas *underneath* the canvas that you have for the user's drawings. – jnpdx Dec 16 '21 at 21:41
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 24 '21 at 05:43

1 Answers1

0

This is an alternate way to resolve this issue. It's work for me.

Let me try to explain with example. Here I have remove only Highlighter and redraw other path on same canvas. so It'll looks like removing only Highlighter not a other text.

I have mention here 4 steps you need to follow

STEP 1> Declare temp variable in class

var tempPKDrawing: PKDrawing?

STEP 2> Now add this PKCanvasViewDelegate methods

 // MARK: Canvas View Delegate
extension PKCanvas: PKCanvasViewDelegate {

  /// Delegate method: Note that the drawing has changed.
  public func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
    print("canvasViewDrawingDidChange\(canvasView.drawing)")
    print ("toolPicker isVisible:\(toolPicker?.isVisible)")

//****** STEP 4> ******
    //Remove Highlighter text
    if canvasView.tool is PKEraserTool, let tempDrawing = self.tempPKDrawing {
      if canvasView.drawing.strokes.count != tempDrawing.strokes.count {
        var newDrawing : [PKStroke] = tempDrawing.strokes.filter({$0.ink.inkType != .marker})
        newDrawing += canvasView.drawing.strokes.filter({$0.ink.inkType == .marker})
        self.tempPKDrawing = nil
        self.canvasView?.drawing = PKDrawing(strokes: newDrawing)
      }
    }

  }

  public func canvasViewDidBeginUsingTool(_ canvasView: PKCanvasView) {
    print("canvasViewDidBeginUsingTool:\(canvasView.drawing)")

//****** STEP 3> ******
    if canvasView.tool is PKEraserTool {
      self.tempPKDrawing = canvasView.drawing
    }
    //canvasView.drawingPolicy = PKEraserToolReference(eraserType: .)
  }
  public func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {
    print("canvasViewDidEndUsingTool")
  }
  public func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) {
    print("canvasViewDidFinishRendering")
  }
}

enter image description here Thanks KOOL ;)

kuldip bhalodiya
  • 992
  • 7
  • 11