forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNGestureHandlerManager.m
190 lines (156 loc) · 6.67 KB
/
RNGestureHandlerManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#import "RNGestureHandlerManager.h"
#import <React/RCTLog.h>
#import <React/RCTViewManager.h>
#import <React/RCTComponent.h>
#import <React/RCTRootView.h>
#import <React/RCTTouchHandler.h>
#import <React/RCTUIManager.h>
#import <React/RCTEventDispatcher.h>
#if __has_include(<React/RCTRootContentView.h>)
#import <React/RCTRootContentView.h>
#else
#import "RCTRootContentView.h"
#endif
#import "RNGestureHandlerState.h"
#import "RNGestureHandler.h"
#import "RNGestureHandlerRegistry.h"
#import "RNRootViewGestureRecognizer.h"
#import "Handlers/RNPanHandler.h"
#import "Handlers/RNTapHandler.h"
#import "Handlers/RNFlingHandler.h"
#import "Handlers/RNLongPressHandler.h"
#import "Handlers/RNNativeViewHandler.h"
#import "Handlers/RNPinchHandler.h"
#import "Handlers/RNRotationHandler.h"
#import "Handlers/RNForceTouchHandler.h"
// We use the method below instead of RCTLog because we log out messages after the bridge gets
// turned down in some cases. Which normally with RCTLog would cause a crash in DEBUG mode
#define RCTLifecycleLog(...) RCTDefaultLogFunction(RCTLogLevelInfo, RCTLogSourceNative, @(__FILE__), @(__LINE__), [NSString stringWithFormat:__VA_ARGS__])
@interface RNGestureHandlerManager () <RNGestureHandlerEventEmitter, RNRootViewGestureRecognizerDelegate>
@end
@implementation RNGestureHandlerManager
{
RNGestureHandlerRegistry *_registry;
RCTUIManager *_uiManager;
NSHashTable<RNRootViewGestureRecognizer *> *_rootViewGestureRecognizers;
RCTEventDispatcher *_eventDispatcher;
}
- (instancetype)initWithUIManager:(RCTUIManager *)uiManager
eventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [super init])) {
_uiManager = uiManager;
_eventDispatcher = eventDispatcher;
_registry = [RNGestureHandlerRegistry new];
_rootViewGestureRecognizers = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
}
return self;
}
- (void)createGestureHandler:(NSString *)handlerName
tag:(NSNumber *)handlerTag
config:(NSDictionary *)config
{
static NSDictionary *map;
static dispatch_once_t mapToken;
dispatch_once(&mapToken, ^{
map = @{
@"PanGestureHandler" : [RNPanGestureHandler class],
@"TapGestureHandler" : [RNTapGestureHandler class],
@"FlingGestureHandler" : [RNFlingGestureHandler class],
@"LongPressGestureHandler": [RNLongPressGestureHandler class],
@"NativeViewGestureHandler": [RNNativeViewGestureHandler class],
@"PinchGestureHandler": [RNPinchGestureHandler class],
@"RotationGestureHandler": [RNRotationGestureHandler class],
@"ForceTouchGestureHandler": [RNForceTouchHandler class],
};
});
Class nodeClass = map[handlerName];
if (!nodeClass) {
RCTLogError(@"Gesture handler type %@ is not supported", handlerName);
return;
}
RNGestureHandler *gestureHandler = [[nodeClass alloc] initWithTag:handlerTag];
[gestureHandler configure:config];
[_registry registerGestureHandler:gestureHandler];
__weak id<RNGestureHandlerEventEmitter> emitter = self;
gestureHandler.emitter = emitter;
}
- (void)attachGestureHandler:(nonnull NSNumber *)handlerTag
toViewWithTag:(nonnull NSNumber *)viewTag
{
UIView *view = [_uiManager viewForReactTag:viewTag];
[_registry attachHandlerWithTag:handlerTag toView:view];
// register view if not already there
[self registerViewWithGestureRecognizerAttachedIfNeeded:view];
}
- (void)updateGestureHandler:(NSNumber *)handlerTag config:(NSDictionary *)config
{
RNGestureHandler *handler = [_registry handlerWithTag:handlerTag];
[handler configure:config];
}
- (void)dropGestureHandler:(NSNumber *)handlerTag
{
[_registry dropHandlerWithTag:handlerTag];
}
- (void)handleSetJSResponder:(NSNumber *)viewTag blockNativeResponder:(NSNumber *)blockNativeResponder
{
if ([blockNativeResponder boolValue]) {
for (RNRootViewGestureRecognizer *recognizer in _rootViewGestureRecognizers) {
[recognizer blockOtherRecognizers];
}
}
}
- (void)handleClearJSResponder
{
// ignore...
}
#pragma mark Root Views Management
- (void)registerViewWithGestureRecognizerAttachedIfNeeded:(UIView *)childView
{
UIView *parent = childView;
while (parent != nil && ![parent respondsToSelector:@selector(touchHandler)]) parent = parent.superview;
// Many views can return the same touchHandler so we check if the one we want to register
// is not already present in the set.
UIView *touchHandlerView = [[parent performSelector:@selector(touchHandler)] view];
if (touchHandlerView == nil) {
return;
}
for (UIGestureRecognizer *recognizer in touchHandlerView.gestureRecognizers) {
if ([recognizer isKindOfClass:[RNRootViewGestureRecognizer class]]) {
return;
}
}
RCTLifecycleLog(@"[GESTURE HANDLER] Initialize gesture handler for view %@", touchHandlerView);
RNRootViewGestureRecognizer *recognizer = [RNRootViewGestureRecognizer new];
recognizer.delegate = self;
touchHandlerView.userInteractionEnabled = YES;
[touchHandlerView addGestureRecognizer:recognizer];
[_rootViewGestureRecognizers addObject:recognizer];
}
- (void)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
didActivateInViewWithTouchHandler:(UIView *)viewWithTouchHandler
{
// Cancel touches in RN's root view in order to cancel all in-js recognizers
// As scroll events are special-cased in RN responder implementation and sending them would
// trigger JS responder change, we don't cancel touches if the handler that got activated is
// a scroll recognizer. This way root view will keep sending touchMove and touchEnd events
// and therefore allow JS responder to properly release the responder at the end of the touch
// stream.
// NOTE: this is not a proper fix and solving this problem requires upstream fixes to RN. In
// particular if we have one PanHandler and ScrollView that can work simultaniously then when
// the Pan handler activates it would still tigger cancel events.
// Once the upstream fix lands the line below along with this comment can be removed
if ([gestureRecognizer.view isKindOfClass:[UIScrollView class]]) return;
RCTTouchHandler *touchHandler = [viewWithTouchHandler performSelector:@selector(touchHandler)];
[touchHandler cancel];
}
#pragma mark Events
- (void)sendTouchEvent:(RNGestureHandlerEvent *)event
{
[_eventDispatcher sendEvent:event];
}
- (void)sendStateChangeEvent:(RNGestureHandlerStateChange *)event
{
[_eventDispatcher sendEvent:event];
}
@end