Erasing strokes

Hello,

currently I am building an UIView in which I'd like to draw over an image.

So far I got the drawing part done like so:

@objc private func didPanWhiteboard(_ gesture: UIPanGestureRecognizer) {
    let location = gesture.location(in: drawCanvasView)
     
    switch gesture.state {
      case .began:
        path = UIBezierPath()
        path.move(to: location)
        strokeLayer = CAShapeLayer()
        drawCanvasView.layer.addSublayer(strokeLayer)
        strokeLayer.strokeColor = toolColor.cgColor
        strokeLayer.fillColor = UIColor.clear.cgColor
        strokeLayer.lineWidth = toolWidth
        strokeLayer.lineJoin = .round
        strokeLayer.lineCap = .round
        strokeLayer.path = path?.cgPath
      case .changed:
        path.addLine(to: location)
        strokeLayer.path = path.cgPath
      case .cancelled, .ended: drawLayers.append(strokeLayer)
      default: break
    }
  }

using a pan gesture.

Now my problem is that I'd like to have an erase tool for my drawing but I am not sure at all on how to actually erase parts of the stroke.

A little bit of Set<> theory. When the draw state is set to erase (this behaviour will have to be modelled), do any current paths intercept any previously-stored paths if yes remove, place in a buffer (for undo purposes) and redraw. So instead of just drawing, you will need to model the behaviours.

Erasing strokes
 
 
Q