Skip to content

Commit f1b1ba8

Browse files
swittkfacebook-github-bot
authored andcommitted
Makes "force" property available to Apple Pencil based events. (#31780)
Summary: Fixes #31779 For more detailed explanation, see issue #31779 React Native touch handler events (onTouchStart, onTouchMoved, etc..) are intended to have "force" properties when used on devices which support pressure input (3D Touch & Apple Pencil events). However, due to a check in RCTForceTouchAvailable() function which checks for UITraitCollection's "forceTouchCapability" to be equal to UIForceTouchCapabilityAvailable, the check returns NO on iPad-based devices, due to iPad devices returning UIForceTouchCapabilityUnavailable at all times. This causes "force" values of Apple Pencils to never be passed on to React Native. Since simply passing 0 as a value for force across the RN bridge for every touch event seemed like a change that might seem jarring to some, I decided that a simple additional boolean check if the touch event's type is UITouchTypePencil (this is the same as UITouchTypeStylus) should also suffice. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [iOS] [Fixed] - Fixed "force" property of touch events for Apple Pencil/Stylus devices. [iOS] [Feature] - Added "altitudeAngle" property to touch events from Apple Pencil/Stylus devices. Pull Request resolved: #31780 Test Plan: The code compiles and runs, and trying a simple handler for a View like ```` touchMove = (e: GestureResponderEvent) => { console.log(`pressure, altitude (${e.nativeEvent.force}, ${e.nativeEvent.altitudeAngle})`); ```` results in <img width="424" alt="Screen Shot 2564-06-28 at 17 13 22" src="https://user-images.githubusercontent.com/5000572/123621055-0b563f00-d835-11eb-9eff-526ba27fdf7b.png"> and when pencil is oriented perpendicular to the screen and pressed with full force shows <img width="412" alt="Screen Shot 2564-06-28 at 17 13 58" src="https://user-images.githubusercontent.com/5000572/123621139-1c06b500-d835-11eb-8207-68a49720d708.png"> Reviewed By: sammy-SC Differential Revision: D29964102 Pulled By: sota000 fbshipit-source-id: 5a1f41d64c6fe325afbd2b9579eaf20a522e92dc
1 parent f592ad0 commit f1b1ba8

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

React/Base/RCTTouchHandler.m

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ - (void)_updateReactTouchAtIndex:(NSInteger)touchIndex
175175
if (RCTForceTouchAvailable()) {
176176
reactTouch[@"force"] = @(RCTZeroIfNaN(nativeTouch.force / nativeTouch.maximumPossibleForce));
177177
}
178+
else if (nativeTouch.type == UITouchTypePencil) {
179+
reactTouch[@"force"] = @(RCTZeroIfNaN(nativeTouch.force / nativeTouch.maximumPossibleForce));
180+
reactTouch[@"altitudeAngle"] = @(RCTZeroIfNaN(nativeTouch.altitudeAngle));
181+
}
178182
}
179183

180184
/**

0 commit comments

Comments
 (0)