Access to lasso tool selection

Is there a way to access the strokes that are being selected by the lasso?

Replies

There is no callback like canvasViewDidSelectStrokes, but we can now inspect the strokes of a PKDrawing. I haven't done this yet, but you could:
  1. Make a SelectionPanGestureRecognizer: UIPanGestureRecognizer that tracks touches along with the PKCanvasView.drawingGestureRecognizer (use UIGestureRecognizerDelegate to allow simultaneous touches, and obviously you only need to track if the selected tool is the lasso).

  2. When the touch is over, create a bounding box of your gesture touches.

  3. Inspect your PKDrawing.strokes array and check for intersection with your bounding box from above.


SelectionPan should track minX, minY, maxX, maxY.

You will use these values to determine your boundingBox: CGRect and then for each PKStroke in PKDrawing.strokes check for an intersection with the PKStroke using boundingBox.intersects($0.renderBounds) to determine if the PKStroke is selected.


Edit: After thinking about it, this will also select PKStrokes that aren't selected by the lasso tool(it's more complex than just a bounding box). It would probably be better to use your own BoundingBox Selection tool and CALayer that draws the selection box, in which case the above would work.


I think my best bet here is to interpolate each stroke's path which is a B-spline into a Bezier path and then intersect the resulting Bezier paths with the path of my custom invisible lasso using SelectionPanGestureRecognizer as you described above.
This library could be helpful in that regard. https://github.com/adamwulf/ClippingBezier
Thank you for your input.
I was wrong in parts of my first reply. You can't use the simple RectSelection to match up with PKLassoTool. You'll need your own overlay, and probably your own ToolPicker at that point. The upside is the selection logic is much simpler and it might be good enough depending on your application.


If you want a complex lasso, like the one in PencilKit, you're gonna need to do a bunch more work! Basically you will need to determine if an "irregular polygon ( the lasso path) contain a point".

You will need to implement some algorithms in swift. I would start with this link reading about the problem.
Stack Overflow


Im pretty sure the solution you posted wouldn't register a path if the selection path encircled it without intersecting it.