Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
private val reactContext: ThemedReactContext
get() = context as ThemedReactContext
private var handlersToAttach: List<Int>? = null
private var nativeHandlersToAttach: MutableSet<Int> = mutableSetOf()
private var attachedNativeHandlers: MutableSet<Int> = mutableSetOf()
private var attachedHandlers: MutableSet<Int> = mutableSetOf()
private var moduleId: Int = -1

Expand Down Expand Up @@ -78,14 +78,15 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
// It might happen that `attachHandlers` will be called before children are added into view hierarchy. In that case we cannot
// attach `NativeViewGestureHandlers` here and we have to do it in `addView` method.
if (shouldAttachGestureToChildView(tag)) {
nativeHandlersToAttach.add(tag)
attachedNativeHandlers.add(tag)
} else {
registry.attachHandlerToView(tag, this.id, GestureHandler.ACTION_TYPE_NATIVE_DETECTOR)

attachedHandlers.add(tag)
}
} else if (entry.value == GestureHandlerMutation.Detach) {
registry.detachHandler(tag)
attachedNativeHandlers.remove(tag)
attachedHandlers.remove(tag)
}
}
Expand All @@ -102,24 +103,20 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
val registry = RNGestureHandlerModule.registries[moduleId]
?: throw Exception("Tried to access a non-existent registry")

for (tag in nativeHandlersToAttach) {
for (tag in attachedNativeHandlers) {
registry.attachHandlerToView(tag, childId, GestureHandler.ACTION_TYPE_NATIVE_DETECTOR)

attachedHandlers.add(tag)
}

nativeHandlersToAttach.clear()
}

private fun detachNativeGestureHandlers() {
val registry = RNGestureHandlerModule.registries[moduleId]
?: throw Exception("Tried to access a non-existent registry")

for (tag in attachedHandlers) {
if (shouldAttachGestureToChildView(tag)) {
registry.detachHandler(tag)
attachedHandlers.remove(tag)
}
for (tag in attachedNativeHandlers) {
registry.detachHandler(tag)
attachedHandlers.remove(tag)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

@interface RNGestureHandlerDetector () <RCTRNGestureHandlerDetectorViewProtocol>

@property (nonatomic, nonnull) NSMutableSet *nativeHandlersToAttach;
@property (nonatomic, nonnull) NSMutableSet *attachedNativeHandlers;
@property (nonatomic, nonnull) NSMutableSet *attachedHandlers;

@end
Expand Down Expand Up @@ -41,7 +41,7 @@ - (instancetype)initWithFrame:(CGRect)frame
static const auto defaultProps = std::make_shared<const RNGestureHandlerDetectorProps>();
_props = defaultProps;
_moduleId = -1;
_nativeHandlersToAttach = [NSMutableSet set];
_attachedNativeHandlers = [NSMutableSet set];
_attachedHandlers = [NSMutableSet set];
}

Expand Down Expand Up @@ -94,11 +94,13 @@ - (BOOL)shouldAttachGestureToSubview:(NSNumber *)handlerTag
return [[[handlerManager registry] handlerWithTag:handlerTag] wantsToAttachDirectlyToView];
}

- (void)addSubview:(UIView *)view
- (void)didAddSubview:(UIView *)view
{
[super addSubview:view];
for (const id handlerTag : _attachedNativeHandlers) {
[self tryAttachHandlerToChildView:handlerTag];
}

[self tryAttachHandlerToChildView];
[super didAddSubview:view];
}

- (void)willRemoveSubview:(UIView *)subview
Expand Down Expand Up @@ -152,7 +154,8 @@ - (void)updateProps:(const Props::Shared &)propsBase oldProps:(const Props::Shar

if (handlerChange.second == RNGestureHandlerMutationAttach) {
if ([self shouldAttachGestureToSubview:handlerTag]) {
[_nativeHandlersToAttach addObject:handlerTag];
[_attachedNativeHandlers addObject:handlerTag];
[self tryAttachHandlerToChildView:handlerTag];
} else {
[handlerManager.registry attachHandlerWithTag:handlerTag
toView:self
Expand All @@ -163,29 +166,23 @@ - (void)updateProps:(const Props::Shared &)propsBase oldProps:(const Props::Shar
} else if (handlerChange.second == RNGestureHandlerMutationDetach) {
[handlerManager.registry detachHandlerWithTag:handlerTag];
[_attachedHandlers removeObject:handlerTag];
[_attachedNativeHandlers removeObject:handlerTag];
}

[self tryAttachHandlerToChildView];
}

[super updateProps:propsBase oldProps:oldPropsBase];
// Override to force hittesting to work outside bounds
self.clipsToBounds = NO;
}

- (void)tryAttachHandlerToChildView
- (void)tryAttachHandlerToChildView:(NSNumber *)handlerTag
{
RNGestureHandlerManager *handlerManager = [RNGestureHandlerModule handlerManagerForModuleId:_moduleId];
[handlerManager.registry attachHandlerWithTag:handlerTag
toView:self.subviews[0]
withActionType:RNGestureHandlerActionTypeNativeDetector];

for (NSNumber *handlerTag in _nativeHandlersToAttach) {
[handlerManager.registry attachHandlerWithTag:handlerTag
toView:self.subviews[0]
withActionType:RNGestureHandlerActionTypeNativeDetector];

[_attachedHandlers addObject:handlerTag];
}

[_nativeHandlersToAttach removeAllObjects];
[_attachedHandlers addObject:handlerTag];
}

- (void)detachNativeGestureHandlers
Expand Down
Loading