I currently try to implement my own lasso tool in Swift in order to change the position of my Strokes drawn on a Pencil Kit Canvas. I select strokes by storing the indices of the strokes that are inside my drawn Rect.
func select(rect: CGRect) {
let path = UIBezierPath(roundedRect: rect, cornerRadius: 5)
for i in 0..<canvas.drawing.strokes.count {
for point in canvas.drawing.strokes[i].path {
if path.contains(point.location) {
selected.append(i)
break
}
}
}
}
I have a DragGesture on this Rect and send the translation to my moveSelected() function.
func moveSelected(val: CGSize) {
for i in selected {
canvas.drawing.strokes[i].transform = CGAffineTransform(translationX: val.width, y: val.height)
}
}
In principle this works very well, but as soon as I select more than two lines the behavior becomes very laggy. The canvasViewDrawingDidChange() gets called every time the moveSelected() function gets called from the .onChange of the DragGesture. Is there a better way to change the position of the strokes to stop the laggy behavior?