When using PencilKit in iOS 13/iPadOS, how do you change the content in PKDrawing? For example, can I go through all the lines and change the color or width of the lines? Or for selected lines (via lasso)? Or add a straight line in our own code?
-
tip: If you are using the PKToolSet you can use the select tool to select strokes. With them selected, chose a new color in the tool bar and it will change those strokes to that color – Adam Meyer Nov 13 '19 at 23:01
-
1…but doing in programmatically? – Lim Thye Chean Nov 14 '19 at 03:02
-
I haven't found a way to do it programmatically - But there might be some hints here: https://github.com/w0lfschild/macOS_headers/blob/master/macOS/PrivateFrameworks/PencilKit/20/PKStrokeSelection.h – Adam Meyer Nov 16 '19 at 00:21
2 Answers
PKDrawing is listed as an Opaque data object. See this Wikipedia Link for an explanation of opaque data object. Unfortunately strokes are not exposed to us. Our only 2 options currently can be found in the docs under Modifying the Drawing. Basically we can only apply a transform or append drawings in the current api. In Apple's Demo, they show how to implement undo using the append method. If you download the demo, see func setNewDrawingUndoable(_ newDrawing: PKDrawing)
to see how you can implement undo.

- 2,610
- 3
- 14
- 19
-
2PKDrawing no longer an opaque data object: https://developer.apple.com/videos/play/wwdc2020/10148 – Parth Mehrotra Feb 06 '21 at 18:08
You can set the built-in selectors undo:
and redo:
to the first responder of the View Controller.
WWDC 19 has a demo has an example that does that in the storyboard itself. Here's the link for you: https://developer.apple.com/documentation/pencilkit/drawing_with_pencilkit

- 1,779
- 1
- 25
- 36
-
Thanks. I have implemented undo/redo... and wonder about the rest. – Lim Thye Chean Oct 12 '19 at 22:29