Skip to content

Commit 67fac20

Browse files
committed
Send touch events on iOS
1 parent e44c585 commit 67fac20

6 files changed

Lines changed: 68 additions & 13 deletions

File tree

packages/react-native-gesture-handler/apple/RNGestureHandler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
withActionType:(RNGestureHandlerActionType)actionType
4343
forRecognizer:(UIGestureRecognizer *)recognizer;
4444

45+
- (void)sendNativeTouchEventForGestureHandler:(RNGestureHandler *)handler withPointerType:(NSInteger)pointerType;
46+
4547
@end
4648

4749
@protocol RNRootViewGestureRecognizerDelegate <UIGestureRecognizerDelegate>

packages/react-native-gesture-handler/apple/RNGestureHandler.mm

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -333,18 +333,22 @@ - (void)sendEvent:(RNGestureHandlerStateChange *)event
333333

334334
- (void)sendTouchEventInState:(RNGestureHandlerState)state forViewWithTag:(NSNumber *)reactTag
335335
{
336-
id extraData = [RNGestureHandlerEventExtraData forEventType:_pointerTracker.eventType
337-
withChangedPointers:_pointerTracker.changedPointersData
338-
withAllPointers:_pointerTracker.allPointersData
339-
withNumberOfTouches:_pointerTracker.trackedPointersCount
340-
withPointerType:_pointerType];
341-
id event = [[RNGestureHandlerEvent alloc] initWithReactTag:reactTag
342-
handlerTag:_tag
343-
state:state
344-
extraData:extraData
345-
coalescingKey:[_tag intValue]];
346-
347-
[self.emitter sendEvent:event withActionType:self.actionType forRecognizer:self.recognizer];
336+
if (self.actionType == RNGestureHandlerActionTypeNativeDetector) {
337+
[self.emitter sendNativeTouchEventForGestureHandler:self withPointerType:_pointerType];
338+
} else {
339+
id extraData = [RNGestureHandlerEventExtraData forEventType:_pointerTracker.eventType
340+
withChangedPointers:_pointerTracker.changedPointersData
341+
withAllPointers:_pointerTracker.allPointersData
342+
withNumberOfTouches:_pointerTracker.trackedPointersCount
343+
withPointerType:_pointerType];
344+
id event = [[RNGestureHandlerEvent alloc] initWithReactTag:reactTag
345+
handlerTag:_tag
346+
state:state
347+
extraData:extraData
348+
coalescingKey:[_tag intValue]];
349+
350+
[self.emitter sendEvent:event withActionType:self.actionType forRecognizer:self.recognizer];
351+
}
348352
}
349353

350354
- (RNGestureHandlerState)recognizerState

packages/react-native-gesture-handler/apple/RNGestureHandlerDetector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ NS_ASSUME_NONNULL_BEGIN
1919

2020
- (void)dispatchGestureEvent:(facebook::react::RNGestureHandlerDetectorEventEmitter::OnGestureHandlerEvent)event;
2121

22+
- (void)dispatchTouchEvent:(facebook::react::RNGestureHandlerDetectorEventEmitter::OnGestureHandlerTouchEvent)event;
23+
2224
@end
2325

2426
NS_ASSUME_NONNULL_END

packages/react-native-gesture-handler/apple/RNGestureHandlerDetector.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ - (void)dispatchGestureEvent:(facebook::react::RNGestureHandlerDetectorEventEmit
7979
}
8080
}
8181

82+
- (void)dispatchTouchEvent:(facebook::react::RNGestureHandlerDetectorEventEmitter::OnGestureHandlerTouchEvent)event
83+
{
84+
if (_eventEmitter != nullptr) {
85+
std::dynamic_pointer_cast<const facebook::react::RNGestureHandlerDetectorEventEmitter>(_eventEmitter)
86+
->onGestureHandlerTouchEvent(event);
87+
}
88+
}
89+
8290
#pragma mark - RCTComponentViewProtocol
8391

8492
+ (ComponentDescriptorProvider)componentDescriptorProvider

packages/react-native-gesture-handler/apple/RNGestureHandlerManager.mm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,42 @@ - (void)sendEvent:(RNGestureHandlerStateChange *)event
400400
}
401401
}
402402

403+
- (void)sendNativeTouchEventForGestureHandler:(RNGestureHandler *)handler withPointerType:(NSInteger)pointerType
404+
{
405+
facebook::react::RNGestureHandlerDetectorEventEmitter::OnGestureHandlerTouchEvent nativeEvent = {
406+
.handlerTag = [handler.tag intValue],
407+
.state = static_cast<int>(handler.state),
408+
.pointerType = static_cast<int>(pointerType),
409+
.numberOfTouches = handler.pointerTracker.trackedPointersCount,
410+
.eventType = static_cast<int>(handler.pointerTracker.eventType),
411+
.changedTouches = {},
412+
.allTouches = {},
413+
};
414+
415+
for (NSDictionary<NSString *, NSNumber *> *touch in handler.pointerTracker.allPointersData) {
416+
nativeEvent.allTouches.push_back({
417+
.id = [[touch valueForKey:@"id"] intValue],
418+
.x = [[touch valueForKey:@"x"] doubleValue],
419+
.y = [[touch valueForKey:@"y"] doubleValue],
420+
.absoluteX = [[touch valueForKey:@"absoluteX"] doubleValue],
421+
.absoluteY = [[touch valueForKey:@"absoluteY"] doubleValue],
422+
});
423+
}
424+
425+
for (NSDictionary<NSString *, NSNumber *> *touch in handler.pointerTracker.changedPointersData) {
426+
nativeEvent.changedTouches.push_back({
427+
.id = [[touch valueForKey:@"id"] intValue],
428+
.x = [[touch valueForKey:@"x"] doubleValue],
429+
.y = [[touch valueForKey:@"y"] doubleValue],
430+
.absoluteX = [[touch valueForKey:@"absoluteX"] doubleValue],
431+
.absoluteY = [[touch valueForKey:@"absoluteY"] doubleValue],
432+
});
433+
}
434+
435+
RNGestureHandlerDetector *detector = (RNGestureHandlerDetector *)handler.recognizer.view;
436+
[detector dispatchTouchEvent:nativeEvent];
437+
}
438+
403439
- (void)sendEventForReanimated:(RNGestureHandlerStateChange *)event
404440
{
405441
// Delivers the event to Reanimated.

packages/react-native-gesture-handler/apple/RNGestureHandlerPointerTracker.m renamed to packages/react-native-gesture-handler/apple/RNGestureHandlerPointerTracker.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "RNGestureHandlerPointerTracker.h"
22
#import "RNGestureHandler.h"
3+
#import "RNGestureHandlerDetector.h"
34

45
#import <React/UIView+React.h>
56

@@ -239,7 +240,9 @@ - (void)sendEvent
239240
// it may happen that the gesture recognizer is reset after it's been unbound from the view,
240241
// it that recognizer tried to send event, the app would crash because the target of the event
241242
// would be nil.
242-
if (!_gestureHandler.needsPointerData || _gestureHandler.recognizer.view.reactTag == nil) {
243+
if (!_gestureHandler.needsPointerData ||
244+
(_gestureHandler.recognizer.view.reactTag == nil &&
245+
![_gestureHandler.recognizer.view isKindOfClass:[RNGestureHandlerDetector class]])) {
243246
return;
244247
}
245248

0 commit comments

Comments
 (0)