PencilKit
From simple drawings to custom creative tools
Talk at SwiftLeeds 2025.
References
- Nutrient iOS SDK
- NSLondon
- PencilKit documentation:
- Using
PKToolPickerwith a custom drawing engine (Nutrient sample code)
Relevant WWDC sessions
- Introducing PencilKit (2019)
- What's new in PencilKit (2020)
- Inspect, modify, and construct PencilKit drawings (2020)
- Squeeze the most out of Apple Pencil (2024)
Code
Basic use of PKCanvasView:
import PlaygroundSupport
import PencilKit
let canvasView = PKCanvasView()
canvasView.tool = PKInkingTool(.crayon, color: .purple, width: 10)
PlaygroundPage.current.liveView = canvasView
Programmatically modifying a drawing to make all stokes red (mentioned, but not shown in talk):
canvasView.drawing.strokes = canvasView.drawing.strokes.map {
var stroke = $0
stroke.ink.color = .red
return stroke
}
Using PKToolPicker with PKCanvasView:
import PlaygroundSupport
import PencilKit
let canvasView = PKCanvasView()
let toolPicker = PKToolPicker()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
PlaygroundPage.current.liveView = canvasView