2

I'd like to change the colors of individual strokes in a drawing. Printing PKDrawing().strokes returns an array with multiple PencilKit.PKStroke items and I can remove strokes from the array which reflects on the canvas. However, if I wanted to change the color of the first stroke for example, how would I go about it?

Thanks!

gelato18
  • 49
  • 8

1 Answers1

5

You need to modify ink color of stroke, like

let canvasView = PKCanvasView() // assuming we some this somewhere above

...

if !canvasView.drawing.strokes.isEmpty {
     // set color whichever needed
     canvasView.drawing.strokes[0].ink.color = UIColor.red  // << here !!
}

Tested with Xcode 12.1 / iOS 14.1

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • For some reason, the color update isn't being reflected in the view. To test it, I added it in an onTapGesture modifier to the parent view and printing out the color for the first stroke shows that the color does get changed. It just isn't updating in the view. Am I putting it in the wrong place? – gelato18 Dec 20 '20 at 05:25
  • See updated... I recognised now drawing is a struct, i.e. value type, so we should access it in-place. – Asperi Dec 20 '20 at 06:24
  • That worked perfectly! Thank you! One last question, how can I add an animated transition between the color change? I tried wrapping the assignment inside withAnimation but that didn't seem to work. – gelato18 Dec 21 '20 at 02:27