# Handling touches from Apple Pencil

**URL:** <https://forums.kodeco.com/t/handling-touches-from-apple-pencil/68923>\
**Category:** iOS\
**Created:** [April 28, 2019, 3:01pm UTC](https://forums.kodeco.com/t/handling-touches-from-apple-pencil/68923 "2019-04-28T15:01:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![feanor](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/feanor/32/81494_2.png) [@feanor](https://forums.kodeco.com/u/feanor)\
**Post date:** [April 28, 2019, 3:01pm UTC](https://forums.kodeco.com/t/handling-touches-from-apple-pencil/68923/1 "2019-04-28T15:01:19Z")

</div>

The force returned from the apple pencil is a value between zero and one. Is it possible to convert it to the actual force with unit Newton? Does Apple mention how to convert it?

---

<div class="post-metadata">

**Author:** ![ericrawn](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ericrawn/32/81494_2.png) [@ericrawn](https://forums.kodeco.com/u/ericrawn)\
**Post date:** [May 21, 2019, 6:38am UTC](https://forums.kodeco.com/t/handling-touches-from-apple-pencil/68923/2 "2019-05-21T06:38:24Z")

</div>

Overview :  
UIKit reports touches from Apple Pencil in the same way it reports touches from the user’s fingers. Specifically, UIKit delivers a UITouch object containing the location of the touch in your app. However, a touch object originating from Apple Pencil contains additional information, including the azimuth and altitude of Apple Pencil and the amount of force recorded at its tip.

Because Apple Pencil is a separate device, there is a delay between the time Apple Pencil gathers altitude, azimuth, and force values and the time that those values are reported to your app. As a result, UIKit may provide estimated values for those properties initially, and then provide the real values at a later time.

When UIKit has only an estimate of a property’s value, it includes a flag in the estimatedPropertiesExpectingUpdates property of the corresponding UITouch object. When handling a touch event, check that property to determine if you need to update the touch information later.

When a touch object contains estimated properties, UIKit also provides a value in the estimationUpdateIndex property that you can use to identify the touch later. Use the value in the estimationUpdateIndex property as a key to a dictionary that you maintain. Set the value of that key to the app-specific object that you use to store the touch information. When UIKit later reports the real values, use the index to look up your app-specific object and replace the estimated values with the real values.

Tracking touches that need updates

var estimates : [NSNumber : StrokeSample]

func addSamples(for touches: [UITouch]) {  
if let stroke = strokeCollection?.activeStroke {  
for touch in touches {  
if touch == touches.last {  
let sample = StrokeSample(point: touch.location(in: self),  
forceValue: touch.force)  
stroke.add(sample: sample)  
registerForEstimates(touch: touch, sample: sample)  
} else {  
let sample = StrokeSample(point: touch.location(in: self),  
forceValue: touch.force, coalesced: true)  
stroke.add(sample: sample)  
registerForEstimates(touch: touch, sample: sample)  
}  
}  
self.setNeedsDisplay()  
}  
}

func registerForEstimates(touch : UITouch, sample : StrokeSample) {  
if touch.estimatedPropertiesExpectingUpdates.contains(.force) {  
estimates[touch.estimationUpdateIndex!] = sample  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [October 12, 2019, 7:11am UTC](https://forums.kodeco.com/t/handling-touches-from-apple-pencil/68923/3 "2019-10-12T07:11:19Z")

</div>

Closed on October 12, 2019, 7:11am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
