Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scroll view handler state change events #2985

Merged
merged 9 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions apple/Handlers/RNNativeViewHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ - (void)touchesBegan:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event

- (void)touchesMoved:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[self updateStateIfScrollView];
[_gestureHandler.pointerTracker touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
{
[_gestureHandler.pointerTracker touchesEnded:touches withEvent:event];
self.state = UIGestureRecognizerStateFailed;
[self reset];

// For now, we are handling only the scroll view case.
// If more views need special treatment, then we can switch to a delegate pattern
if ([_gestureHandler retrieveScrollView:self.view] == nil) {
[self reset];
}
}

- (void)touchesCancelled:(NSSet<RNGHUITouch *> *)touches withEvent:(UIEvent *)event
Expand All @@ -69,6 +75,19 @@ - (void)reset
[_gestureHandler reset];
}

- (void)updateStateIfScrollView
{
UIScrollView *scrollView = [_gestureHandler retrieveScrollView:self.view];
if (!scrollView) {
return;
}
for (UIGestureRecognizer *scrollViewGestureRecognizer in scrollView.gestureRecognizers) {
if ([_gestureHandler isUIScrollViewPanGestureRecognizer:scrollViewGestureRecognizer]) {
self.state = scrollViewGestureRecognizer.state;
}
}
}

@end

#pragma mark RNNativeViewGestureHandler
Expand Down Expand Up @@ -116,20 +135,8 @@ - (void)bindToView:(UIView *)view
// We can restore default scrollview behaviour to delay touches to scrollview's children
// because gesture handler system can handle cancellation of scroll recognizer when JS responder
// is set
#ifdef RCT_NEW_ARCH_ENABLED
if ([view isKindOfClass:[RCTScrollViewComponentView class]]) {
UIScrollView *scrollView = ((RCTScrollViewComponentView *)view).scrollView;
scrollView.delaysContentTouches = YES;
}
#else
if ([view isKindOfClass:[RCTScrollView class]]) {
// This part of the code is coupled with RN implementation of ScrollView native wrapper and
// we expect for RCTScrollView component to contain a subclass of UIScrollview as the only
// subview
UIScrollView *scrollView = [view.subviews objectAtIndex:0];
scrollView.delaysContentTouches = YES;
}
#endif // RCT_NEW_ARCH_ENABLED
UIScrollView *scrollView = [self retrieveScrollView:view];
scrollView.delaysContentTouches = YES;
}

- (void)handleTouchDown:(UIView *)sender forEvent:(UIEvent *)event
Expand Down
13 changes: 13 additions & 0 deletions apple/RNGestureHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
#import <Foundation/Foundation.h>
#import <React/RCTConvert.h>

#if TARGET_OS_OSX
#define RNGHUIScrollView NSScrollView
#else
#define RNGHUIScrollView UIScrollView
#endif

#define VEC_LEN_SQ(pt) (pt.x * pt.x + pt.y * pt.y)
#define TEST_MIN_IF_NOT_NAN(value, limit) \
(!isnan(limit) && ((limit < 0 && value <= limit) || (limit >= 0 && value >= limit)))
Expand Down Expand Up @@ -89,6 +95,13 @@
withExtraData:(nonnull RNGestureHandlerEventExtraData *)extraData;
- (void)sendEvent:(nonnull RNGestureHandlerStateChange *)event;
- (void)sendTouchEventInState:(RNGestureHandlerState)state forViewWithTag:(nonnull NSNumber *)reactTag;
- (nullable RNGHUIScrollView *)retrieveScrollView:(nonnull RNGHUIView *)view;

#if !TARGET_OS_OSX
- (BOOL)isUIScrollViewPanGestureRecognizer:(nonnull UIGestureRecognizer *)gestureRecognizer;
#else
- (BOOL)isUIScrollViewPanGestureRecognizer:(nonnull NSGestureRecognizer *)gestureRecognizer;
#endif

#if !TARGET_OS_OSX
- (void)setCurrentPointerType:(nonnull UIEvent *)event;
Expand Down
64 changes: 63 additions & 1 deletion apple/RNGestureHandler.m → apple/RNGestureHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@

#if !TARGET_OS_OSX
#import <UIKit/UIGestureRecognizerSubclass.h>
#import <UIKit/UIPanGestureRecognizer.h>
#endif

#import <React/UIView+React.h>

#ifdef RCT_NEW_ARCH_ENABLED
#import <React/RCTScrollViewComponentView.h>
#else
#import <React/RCTScrollView.h>
#endif

@interface UIGestureRecognizer (GestureHandler)
@property (nonatomic, readonly) RNGestureHandler *gestureHandler;
@end
Expand Down Expand Up @@ -205,7 +212,7 @@ - (void)setCurrentPointerTypeToMouse

- (UITouchType)getPointerType
{
return _pointerType;
return (UITouchType)_pointerType;
}

- (void)bindToView:(RNGHUIView *)view
Expand Down Expand Up @@ -474,6 +481,10 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
return YES;
}

if ([self areScrollViewRecognizersCompatible:gestureRecognizer otherRecognizer:otherGestureRecognizer]) {
return YES;
}

RNGestureHandler *handler = [RNGestureHandler findGestureHandlerByRecognizer:otherGestureRecognizer];
if (handler != nil) {
if ([_simultaneousHandlers count]) {
Expand All @@ -495,6 +506,57 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
return NO;
}

- (BOOL)areScrollViewRecognizersCompatible:(UIGestureRecognizer *)gestureRecognizer
otherRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([self isUIScrollViewPanGestureRecognizer:otherGestureRecognizer] &&
[gestureRecognizer isKindOfClass:[RNDummyGestureRecognizer class]]) {
RNGHUIScrollView *scrollView = [self retrieveScrollView:gestureRecognizer.view];
if (scrollView && scrollView == otherGestureRecognizer.view) {
return YES;
}
}

return NO;
}

#if !TARGET_OS_OSX
// is UIPanGestureRecognizer and has scrollView property
- (BOOL)isUIScrollViewPanGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
{
return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] &&
[gestureRecognizer respondsToSelector:@selector(scrollView)];
}

#else

- (BOOL)isUIScrollViewPanGestureRecognizer:(NSGestureRecognizer *)gestureRecognizer
{
return NO;
}

#endif

- (RNGHUIScrollView *)retrieveScrollView:(RNGHUIView *)view
{
#ifdef RCT_NEW_ARCH_ENABLED
if ([view isKindOfClass:[RCTScrollViewComponentView class]]) {
RNGHUIScrollView *scrollView = ((RCTScrollViewComponentView *)view).scrollView;
return scrollView;
}
#else
if ([view isKindOfClass:[RCTScrollView class]]) {
// This part of the code is coupled with RN implementation of ScrollView native wrapper and
// we expect for RCTScrollView component to contain a subclass of UIScrollview as the only
// subview
RNGHUIScrollView *scrollView = [view.subviews objectAtIndex:0];
return scrollView;
}
#endif

return nil;
}

- (void)reset
{
// do not reset states while gesture is tracking pointers, as gestureRecognizerShouldBegin
Expand Down
Loading