Search code examples
iosswiftswiftuiios14pencilkit

How to modify individual strokes in PKDrawing? - PencilKit / SwiftUI


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!


Solution

  • 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