From f1b1ba8963ff152d995c3cd132bc0755413bc44f Mon Sep 17 00:00:00 2001 From: swittk Date: Wed, 28 Jul 2021 18:34:45 -0700 Subject: [PATCH] Makes "force" property available to Apple Pencil based events. (#31780) Summary: Fixes https://github.com/facebook/react-native/issues/31779 For more detailed explanation, see issue https://github.com/facebook/react-native/issues/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 [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: https://github.com/facebook/react-native/pull/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 Screen Shot 2564-06-28 at 17 13 22 and when pencil is oriented perpendicular to the screen and pressed with full force shows Screen Shot 2564-06-28 at 17 13 58 Reviewed By: sammy-SC Differential Revision: D29964102 Pulled By: sota000 fbshipit-source-id: 5a1f41d64c6fe325afbd2b9579eaf20a522e92dc --- React/Base/RCTTouchHandler.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/React/Base/RCTTouchHandler.m b/React/Base/RCTTouchHandler.m index bfcadc3d607335..d1754c93e158d2 100644 --- a/React/Base/RCTTouchHandler.m +++ b/React/Base/RCTTouchHandler.m @@ -175,6 +175,10 @@ - (void)_updateReactTouchAtIndex:(NSInteger)touchIndex if (RCTForceTouchAvailable()) { reactTouch[@"force"] = @(RCTZeroIfNaN(nativeTouch.force / nativeTouch.maximumPossibleForce)); } + else if (nativeTouch.type == UITouchTypePencil) { + reactTouch[@"force"] = @(RCTZeroIfNaN(nativeTouch.force / nativeTouch.maximumPossibleForce)); + reactTouch[@"altitudeAngle"] = @(RCTZeroIfNaN(nativeTouch.altitudeAngle)); + } } /**