Drawing with PencilKit: Getting Started

In this PencilKit tutorial, you’ll learn to use PencilKit and an application where users can draw on a canvas using provided tools. By Christine Abernathy.

4.7 (17) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Advanced PencilKit

You’ve seen how to use PKDrawing to clear the canvas and save a drawing.
You can change PKDrawing to suit your needs. For example, you might want to create a drawing on one device and display on another with a different canvas size. You can perform a CGAffineTransform on the drawing to do this. For example:

let transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
let drawingTransformed = pkDrawing.transformed(using: transform)

This code scales down a drawing by 50 percent.

You can even append one drawing to another by doing something like this:

let drawingCombined = pkDrawing1.appending(pkDrawing2)

Double-tapping the lower part of the Apple Pencil changes the tool selected. Try it out if you can. By default, it switches between the eraser and the currently selected tool. Users can go to Settings and change how they want double-tap to work. If your app has custom double-tap support, make sure you don’t confuse users if they’re used to a certain behavior. Give them the ability to opt in to your custom behavior.

Apple Pencil can sense tilt, force and orientation. By default, this changes the stroke. For example, applying more force leads to thicker lines. If you customize the tool picker, apply these data when drawing strokes. This Apple Pencil Tutorial shows an example of how to do this.

One other thing to be mindful of is that users can turn on dark mode. By default, colors in the PencilKit canvas adjust based on dark mode settings. Think about preventing automatic color changes for apps where users draw on top of a PDS or photo. This makes sure the markup remains visible.

Where to Go From Here?

Congratulations on completing the PencilKit tutorial! As you’ve seen, it’s very easy to build an app with drawing capabilities that gives users a lot of room to get creative.

Download the completed version of the project using the Download Materials button. You can find this at the top and bottom of this tutorial.

Check out the Apple Pencil Tutorial for a deeper look into what you can do with Apple Pencil. Introducing PencilKit from WWDC 2019 gives a good overview of the various features. For new updates to PencilKit framework in iOS 14, make sure you check out What’s new in PencilKit session from WWDC 2020.

I hope you enjoyed this tutorial! If you have any questions or comments, please join the discussion below.