Skip to content

Commit 6dddda8

Browse files
authored
Fixing number of touches becoming 0 issue for pan gestures in apple track pad (#3865)
Currently the track pad pinch gesture returns the number of touch points to 0 from native method. This causes the focal X and Y to become NaN. This fixes the issue reported in #3864
1 parent 6e8646a commit 6dddda8

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

packages/react-native-gesture-handler/apple/Handlers/RNPinchHandler.m

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,28 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(NSMagnificationGestureRecogn
163163
#else
164164
- (RNGestureHandlerEventExtraData *)eventExtraData:(UIPinchGestureRecognizer *)recognizer
165165
{
166-
CGPoint accumulatedPoint = CGPointZero;
167-
168-
for (int i = 0; i < recognizer.numberOfTouches; i++) {
169-
CGPoint location = [recognizer locationOfTouch:i inView:recognizer.view];
170-
accumulatedPoint.x += location.x;
171-
accumulatedPoint.y += location.y;
166+
CGPoint focalPoint;
167+
NSUInteger numberOfTouches = recognizer.numberOfTouches;
168+
169+
if (numberOfTouches > 0) {
170+
CGPoint accumulatedPoint = CGPointZero;
171+
172+
for (int i = 0; i < numberOfTouches; i++) {
173+
CGPoint location = [recognizer locationOfTouch:i inView:recognizer.view];
174+
accumulatedPoint.x += location.x;
175+
accumulatedPoint.y += location.y;
176+
}
177+
178+
focalPoint = CGPointMake(accumulatedPoint.x / numberOfTouches, accumulatedPoint.y / numberOfTouches);
179+
} else {
180+
// Trackpad pinch gestures may report 0 touches - use the recognizer's location instead
181+
focalPoint = [recognizer locationInView:recognizer.view];
172182
}
173183

174-
CGPoint focalPoint =
175-
CGPointMake(accumulatedPoint.x / recognizer.numberOfTouches, accumulatedPoint.y / recognizer.numberOfTouches);
176-
177184
return [RNGestureHandlerEventExtraData forPinch:recognizer.scale
178185
withFocalPoint:focalPoint
179186
withVelocity:recognizer.velocity
180-
withNumberOfTouches:recognizer.numberOfTouches
187+
withNumberOfTouches:numberOfTouches
181188
withPointerType:_pointerType];
182189
}
183190
#endif

0 commit comments

Comments
 (0)