-
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add category on UIView with code for touch handler finding
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#import <Foundation/Foundation.h> | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
#import <React/RCTSurfaceTouchHandler.h> | ||
#else | ||
#import <React/RCTTouchHandler.h> | ||
#endif // RCT_NEW_ARCH_ENABLED | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
#define RNS_TOUCH_HANDLER_ARCH_TYPE RCTSurfaceTouchHandler | ||
#else | ||
#define RNS_TOUCH_HANDLER_ARCH_TYPE RCTTouchHandler | ||
#endif // RCT_NEW_ARCH_ENABLED | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface UIView (RNSUtility) | ||
|
||
- (nullable RNS_TOUCH_HANDLER_ARCH_TYPE *)rnscreens_findTouchHandlerInAncestorChain; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
#import "UIView+RNSUtility.h" | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
#import <React/RCTSurfaceView.h> | ||
#endif | ||
|
||
@implementation UIView (RNSUtility) | ||
|
||
- (nullable RNS_TOUCH_HANDLER_ARCH_TYPE *)rnscreens_findTouchHandlerInAncestorChain | ||
{ | ||
UIView *parent = self; | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
// On Fabric there is no view that exposes touchHandler above us in the view hierarchy, however it is still | ||
// utilised. `RCTSurfaceView` should be present above us, which hosts `RCTFabricSurface` instance, which in turn | ||
// hosts `RCTSurfaceTouchHandler` as a private field. When initialised, `RCTSurfaceTouchHandler` is attached to the | ||
// surface view as a gestureRecognizer <- and this is where we can lay our hands on it. | ||
|
||
while (parent != nil && ![parent isKindOfClass:RCTSurfaceView.class]) { | ||
parent = parent.superview; | ||
} | ||
|
||
// This could be possible in modal context | ||
if (parent == nil) { | ||
return nil; | ||
} | ||
|
||
// Experimentation shows that RCTSurfaceTouchHandler is the only gestureRecognizer registered here, | ||
// so we should not be afraid of any performance hit here. | ||
for (UIGestureRecognizer *recognizer in parent.gestureRecognizers) { | ||
if ([recognizer isKindOfClass:RCTSurfaceTouchHandler.class]) { | ||
return static_cast<RNS_TOUCH_HANDLER_ARCH_TYPE *>(recognizer); | ||
} | ||
} | ||
|
||
#else | ||
|
||
// On Paper we can access touchHandler hosted by `RCTRootContentView` which should be above ScreenStack | ||
// in view hierarchy. | ||
while (parent != nil && ![parent respondsToSelector:@selector(touchHandler)]) { | ||
parent = parent.superview; | ||
} | ||
|
||
if (parent != nil) { | ||
RCTTouchHandler *touchHandler = [parent performSelector:@selector(touchHandler)]; | ||
return static_cast<RNS_TOUCH_HANDLER_ARCH_TYPE *>(touchHandler); | ||
} | ||
|
||
#endif // RCT_NEW_ARCH_ENABLED | ||
|
||
return nil; | ||
} | ||
|
||
@end |