# Apple Pencil Tutorial: Getting Started

**URL:** <https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880>\
**Category:** 🎓 Courses & Articles\
**Created:** [March 4, 2016, 12:20pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880 "2016-03-04T12:20:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [March 4, 2016, 12:20pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/1 "2016-03-04T12:20:11Z")

</div>

In this Apple Pencil tutorial, you'll learn about force, touch coalescing, altitude, and azimuth, to add realistic lines and shading to a drawing app.

* * *
 This is a companion discussion topic for the original entry at [https://www.raywenderlich.com/1407-apple-pencil-tutorial-getting-started](https://www.raywenderlich.com/1407-apple-pencil-tutorial-getting-started)

---

<div class="post-metadata">

**Author:** ![dkliman](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dkliman/32/69829_2.png) [@dkliman](https://forums.kodeco.com/u/dkliman)\
**Post date:** [April 28, 2016, 4:35pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/2 "2016-04-28T16:35:47Z")

</div>

Hi, Caroline,

This was a really nice tutorial, and it reminds me I’m still interested in this one part:

> you had to do complex things like convert your strokes to spline curves to make drawings look decent

I’d really like to know how to do that, especially to make a blek style line, or one that let’s say thickens and burns as the swipe happens, and thins out towards its constantly moving tail…

even though the coalescing touches added a bit of resolution, i could still see those straight lines between points that I’d prefer to be curved.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [April 29, 2016, 2:16am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/3 "2016-04-29T02:16:40Z")

</div>

You’ll need to get familiar with Bezier curves.  
This is the best article I’ve found on drawing splines and varying width dependent on velocity:

> **[Drawing smooth lines with cocos2d ios inspired by Paper](https://www.merowing.info/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/)**
>
> You’ve probably seen Paper, the app is pretty cool and the drawings look really nice and smooth.
> 
> I’m working on my personal app that needs something similar, and after doing some research I’ve not seen a proper solution.
> 
> So I wrote...

There’s also a drawing tutorial here:

> **[Smooth Freehand Drawing on iOS](https://code.tutsplus.com/tutorials/smooth-freehand-drawing-on-ios--mobile-13164)**
>
> This tutorial will teach you how to implement an advanced drawing algorithm for smooth, freehand drawing on iOS devices. Read on!
> 
> Theoretical Overview
> Touch is the primary way a user will...

which shows how to go from shaky writing to smooth writing.

This is a good article on Bezier curves:

> **[ronnqvi.st - This website is for sale! - ronnqvi...](http://ww16.ronnqvi.st/thinking-like-a-bzier-path/?sub1=20221025-0710-116d-9fd5-f8518c72d951)**
>
> This website is for sale! ronnqvi.st is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, ronnqvi.st has it all. We hope you find what you are searching for!

but there’s a ton more articles out there on Bezier curves.

---

<div class="post-metadata">

**Author:** ![dkliman](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dkliman/32/69829_2.png) [@dkliman](https://forums.kodeco.com/u/dkliman)\
**Post date:** [April 29, 2016, 5:04am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/4 "2016-04-29T05:04:28Z")

</div>

Thanks for the interesting links!

So sad that it’s all in objective C ☹

luckily i know about bezier paths and control points… i just need to understand the syntax of handling the array of touches that is built and used to draw the chunks of line…

I guess I’ll play with that tutorial and see if [swiftify.co](http://swiftify.co) is helpful 🙂

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [May 2, 2016, 10:43pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/5 "2016-05-02T22:43:50Z")

</div>

I didn’t know about [swiftify.co](http://swiftify.co). What a great idea! Having said that, in my opinion, it’s a good idea to muddle through Objective C sites because there are a lot of valuable Objective C tutorials and projects out there. And in most cases, it’s a simple translation.

---

<div class="post-metadata">

**Author:** ![dkliman](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dkliman/32/69829_2.png) [@dkliman](https://forums.kodeco.com/u/dkliman)\
**Post date:** [May 3, 2016, 7:30am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/6 "2016-05-03T07:30:18Z")

</div>

that’s a good point… I’m trying not to fill my head with too much C but perhaps a little wouldn’t be horrid. I’m still interested in finding out how to make a blek (app) style line, taking advantage of all the coalescing touches and so forth.

also interesting to me is the audio synth abilities of iOS, especially by making the sounds blek does when drawing…

---

<div class="post-metadata">

**Author:** ![mittalvishesh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mittalvishesh/32/35417_2.png) [@mittalvishesh](https://forums.kodeco.com/u/mittalvishesh)\
**Post date:** [June 26, 2016, 3:10am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/7 "2016-06-26T03:10:41Z")

</div>

This is a great tutorial! I am new to Swift/iOS, and am learning a lot.

Was wondering, is there a way to have a background image in this? I tried setting canvasView.image, but obviously that wont work.

Also, the eraser is basically a white pencil - which wont work if there is a background present.

Great job on this tutorial!! 🙂

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [June 27, 2016, 11:52pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/8 "2016-06-27T23:52:03Z")

</div>

You can have an image view under the canvas view, and set the canvas view’s background color to clear color.

Yes - I faked the eraser - the tutorial was focussed on using Pencil rather than making a drawing app. But something like this might work:

In `drawStroke(_:touch:)` before stroking the path at the end:

```auto
   if touch.type != .Stylus {
      let color = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
      color.setStroke()
      CGContextSetBlendMode(context, .Clear)
    }

```

And thanks for the kind words 😄

---

<div class="post-metadata">

**Author:** ![mittalvishesh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mittalvishesh/32/35417_2.png) [@mittalvishesh](https://forums.kodeco.com/u/mittalvishesh)\
**Post date:** [June 28, 2016, 4:09pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/9 "2016-06-28T16:09:32Z")

</div>

Hi Caroline!  
Thank you so much for your reply. This is amazing - so much to learn! I will be subscribing to your video tutorials shortly!

---

<div class="post-metadata">

**Author:** ![bleckett](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bleckett/32/92287_2.png) [@bleckett](https://forums.kodeco.com/u/bleckett)\
**Post date:** [October 23, 2016, 4:24pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/10 "2016-10-23T16:24:45Z")

</div>

Thank you for your GREAT tutorial.

> [@](#):
>
> But how would I keep the drawing from resizing when rotating the screen?

  
I'd like the drawn image to stay the same size in both portrait and landscape.

The code works great for when the **ContentMode** is set to **.ScaleToFill**.  
But if I set the **ContentMode** to a non-scaled mode like **.TopRight** and rotate the screen, the image proportions change when touchesMoved is fired. I think it has to do with placing the **drawingImage** into the context using a bounds that was different from the previous rotation, but I can’t figure out how get it to draw with the correct proportions.

I’ve tried storing and using a previous bounds (before rotation), setting the **ContentMode** before and after adding the **drawingImage** to the context, and even looking at manipulating cg/ci Image data using extent, etc…

Any suggestions?

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [October 24, 2016, 1:41am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/11 "2016-10-24T01:41:44Z")

</div>

@bleckett - I’ve spent a bit of time with this one. I’m not sure I’ve done it the best way though.

I am assuming you want to do something like this:

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/2/e/2e0b6be8a2924ba5a796c80e5baa58d2be255d3d.png)

That’s where the painted view does not rotate, but any other views do. If you just want to turn off rotation for the whole app, you can do it in the Project Target settings as the Scribble starter project does.

I’ve taken a combination of this:

> <https://stackoverflow.com/questions/2191522/disabling-autorotate-for-a-single-uiview/10658860#10658860>

and this:

> **[Technical Q&A QA1890: Preventing a View From Rotating](https://developer.apple.com/library/archive/qa/qa1890/_index.html)**
>
> QA1890: describes how to 'lock' the orientation of a view while allowing the rest of the interface to rotate.

I couldn’t get the Apple suggestion to do what I wanted it to do, but removing auto layout from the canvasView and putting it into a xib allowed me to rotate it as per the stackOverflow link.

The attached zip is my suggestion. (I changed eraser color to green for testing on the simulator.)

[Scribble-Working Rotation.zip](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/d/3/d32bb7171d458657b250e62f995421f8dca62371.zip) (1.1 MB)

---

<div class="post-metadata">

**Author:** ![bleckett](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bleckett/32/92287_2.png) [@bleckett](https://forums.kodeco.com/u/bleckett)\
**Post date:** [October 25, 2016, 12:46am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/12 "2016-10-25T00:46:50Z")

</div>

Thank you. I didn’t think of using **CGAffineTransform**!  
I did remove auto layout for the canvas view and set the contentMode to topRight (and extended the view so that it covers both landscape and portrait). But was hoping that there was some trick with drawing into the context.

Thank you again.

---

<div class="post-metadata">

**Author:** ![lyndoncruz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lyndoncruz/32/47573_2.png) [@lyndoncruz](https://forums.kodeco.com/u/lyndoncruz)\
**Post date:** [November 14, 2016, 2:32pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/13 "2016-11-14T14:32:12Z")

</div>

Hi Caroline,

This is a great tutorial! Thank you so much.

I was wondering how can we save and edit previous drawing.

I used this to save the drawing:

let data = UIImagePNGRepresentation(canvasView.image!)  
let userDefaults = UserDefaults.standard  
userDefaults.set(data, forKey: “test1”)  
userDefaults.synchronize()

And used this to retrieve the files:  
if let data = UserDefaults.standard.data(forKey: “test123”),let image = UIImage(data: data,scale:1.0){  
canvasView.image = image  
}

But when i started to edit retrieve files all drawing has been deleted.

Any tips. Thanks.

Best Regards,

Lyndon

---

<div class="post-metadata">

**Author:** ![daviddelmonte](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daviddelmonte/32/68124_2.png) [@daviddelmonte](https://forums.kodeco.com/u/daviddelmonte)\
**Post date:** [December 2, 2016, 4:03am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/14 "2016-12-02T04:03:16Z")

</div>

Hi Caroline. I wanted to thank you too for this very valuable tutorial. I had no problems building a swift drawing app as an exercise. I am stuck for now with an Objective-C drawing engine in my main project. I cant seem to find a tutorial as in-depth as yours based in ObjC. Is there a reason for that? I wonder if you point me (semi-pun) in the right direction.

Many thanks!

David

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [December 2, 2016, 9:09am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/15 "2016-12-02T09:09:46Z")

</div>

@daviddelmonte - thank you :].

I don’t know of any modern tutorials being written in Objective-C - all tutorial sites that I can think of have moved to Swift.

[www.raywenderlich.com](http://www.raywenderlich.com) has been going for years, so the older tutorials are written in Objective-C and they should come up with Google. Anything dated before June 2014.

For example there’s this Core Graphics drawing series by Ray: [https://www.raywenderlich.com/32283/core-graphics-tutorial-lines-rectangles-and-gradients](https://www.raywenderlich.com/32283/core-graphics-tutorial-lines-rectangles-and-gradients)

Edit: The reason for no Objective-C is that Swift is the way forward. There will still be a large code-base of Objective-C apps, but companies are gradually switching to Swift. It’s faster to run (generally) and faster to code (once you know the language).

---

<div class="post-metadata">

**Author:** ![daviddelmonte](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daviddelmonte/32/68124_2.png) [@daviddelmonte](https://forums.kodeco.com/u/daviddelmonte)\
**Post date:** [December 10, 2016, 6:08pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/16 "2016-12-10T18:08:28Z")

</div>

You are right of course. Time to change the engine to a swift based version. I hope the client will go for that. I’ll look through the tute list to see if something will make it easier. I do appreciate you getting back to me…  
David

---

<div class="post-metadata">

**Author:** ![daviddelmonte](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/daviddelmonte/32/68124_2.png) [@daviddelmonte](https://forums.kodeco.com/u/daviddelmonte)\
**Post date:** [December 10, 2016, 6:36pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/17 "2016-12-10T18:36:01Z")

</div>

Sorry, another question… You are using a UIImageView as your base class. Was there a reason you do not use UIView? thanks again!

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [December 10, 2016, 6:48pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/18 "2016-12-10T18:48:04Z")

</div>

@daviddelmonte - UIImageView is a subclass of UIView, and it draws an image automatically.

---

<div class="post-metadata">

**Author:** ![denisha](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/denisha/32/68452_2.png) [@denisha](https://forums.kodeco.com/u/denisha)\
**Post date:** [May 29, 2017, 4:32pm UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/19 "2017-05-29T16:32:05Z")

</div>

Hello Caroline. I have tried to implement this in my app, but it is not working as desired.  
I am always getting zero value in “touch.force”.  
Please help. Thanks…

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [June 3, 2017, 2:29am UTC](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880/20 "2017-06-03T02:29:05Z")

</div>

I’ve downloaded and converted the sample code to Swift 3 and run it on my iPad Pro.

I’ve added this:

```
print("force: \(touch.force)")

```

to `lineWidthForDrawing()`, and it successfully prints out non-zero values.

`touch.force` will only produce values if you use Apple Pencil on an iPad Pro. Are you doing this? If so, does the sample code work for you?

[Next page](https://forums.kodeco.com/t/apple-pencil-tutorial-getting-started/6880.md?page=2)
