Skip to content

Commit 7b35938

Browse files
m-bertCopilot
andauthored
[iOS] Fix touch events never being delivered to VirtualDetector handlers (#4392)
## Description On iOS, handlers attached through `VirtualGestureDetector` never received `onTouches*` callbacks: state events (`onBegin`/`onActivate`/`onFinalize`) arrived, but every pointer-data event was silently dropped. Android and web deliver all of them for the same code. Two spots in the touch-event path were only half-migrated when the virtual detector joined the native one — both still special-cased `RNGestureHandlerActionTypeNativeDetector` alone: 1. `RNGestureHandler.sendTouchEventInState` routed touch events through the codegen detector path (`sendNativeTouchEventForGestureHandler`, which resolves its target via `findViewForEvents` → `hostDetectorView`) only for the native detector. A virtual handler's touch events fell into the legacy branch that builds a `reactTag`-routed event. 2. `RNGestureHandlerPointerTracker.sendEvent` drops events when the recognizer's view has no `reactTag`, with an exemption only for the native detector. The virtual recognizer's view has no `reactTag`, so the guard swallowed every touch event for virtual handlers unconditionally. Both checks now use the existing `usesNativeOrVirtualDetector` helper, mirroring how Android's `RNGestureHandlerEventDispatcher` treats the two detector types uniformly (single branch, delivery through the detector view). With (1) in place, detector-attached touch events never involve the view's `reactTag`, which is what makes the widened exemption in (2) safe. ## Test plan <details> <summary>Tested on the following code:</summary> ```tsx import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; import { GestureDetector, InterceptingGestureDetector, usePanGesture, useTapGesture, VirtualGestureDetector, } from 'react-native-gesture-handler'; export default function EmptyExample() { const interceptingTap = useTapGesture({ disableReanimated: true }); const virtualTapWithTouches = useTapGesture({ disableReanimated: true, onActivate: () => console.log('[virtual-tap-touches] onActivate'), onFinalize: () => console.log('[virtual-tap-touches] onFinalize'), onTouchesDown: () => console.log('[virtual-tap-touches] onTouchesDown'), onTouchesUp: () => console.log('[virtual-tap-touches] onTouchesUp'), }); const nativePan = usePanGesture({ disableReanimated: true, onBegin: () => console.log('[native] onBegin'), onActivate: () => console.log('[native] onActivate'), onDeactivate: () => console.log('[native] onDeactivate'), onFinalize: () => console.log('[native] onFinalize'), onTouchesDown: () => console.log('[native] onTouchesDown'), onTouchesMove: () => console.log('[native] onTouchesMove'), onTouchesUp: () => console.log('[native] onTouchesUp'), onTouchesCancel: () => console.log('[native] onTouchesCancel'), }); return ( <View style={styles.container}> <InterceptingGestureDetector gesture={interceptingTap}> <Text style={styles.paragraph}> Virtual detector target:{' '} <VirtualGestureDetector gesture={virtualTapWithTouches}> <Text style={styles.virtualTarget}>TAP ME</Text> </VirtualGestureDetector> </Text> </InterceptingGestureDetector> <GestureDetector gesture={nativePan}> <View style={styles.box}> <Text style={styles.boxLabel}>native detector control</Text> </View> </GestureDetector> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', gap: 24, padding: 24 }, paragraph: { fontSize: 18, textAlign: 'center' }, virtualTarget: { fontSize: 22, fontWeight: 'bold', color: 'darkgreen' }, box: { width: 220, height: 100, borderRadius: 12, backgroundColor: 'steelblue', justifyContent: 'center', alignItems: 'center', }, boxLabel: { color: 'white' }, }); ``` </details> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 73c51c7 commit 7b35938

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ - (void)sendEvent:(RNGestureHandlerStateChange *)event
596596

597597
- (void)sendTouchEventInState:(RNGestureHandlerState)state forViewWithTag:(NSNumber *)reactTag
598598
{
599-
if (_actionType == RNGestureHandlerActionTypeNativeDetector) {
599+
if ([self usesNativeOrVirtualDetector]) {
600600
[self.emitter sendNativeTouchEventForGestureHandler:self
601601
withPointerType:_pointerType
602602
forHandlerType:[self eventHandlerType]];

‎packages/react-native-gesture-handler/apple/RNGestureHandlerPointerTracker.mm‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,12 @@ - (void)sendEvent
296296
}
297297

298298
// it may happen that the gesture recognizer is reset after it's been unbound from the view,
299-
// it that recognizer tried to send event, the app would crash because the target of the event
300-
// would be nil.
301-
if (_gestureHandler.recognizer.view.reactTag == nil &&
302-
_gestureHandler.actionType != RNGestureHandlerActionTypeNativeDetector) {
299+
// if that recognizer tried to send an event, the app would crash because the target of the event
300+
// would be nil. Detector-attached handlers (native and virtual alike) don't route events by
301+
// the view's reactTag — they dispatch through the detector view — so a nil tag is not an
302+
// obstacle for them. The virtual recognizer's view has no reactTag at all, so without this
303+
// exemption virtual handlers would never deliver touch events.
304+
if (_gestureHandler.recognizer.view.reactTag == nil && ![_gestureHandler usesNativeOrVirtualDetector]) {
303305
return;
304306
}
305307

0 commit comments

Comments
 (0)