Skip to content

Commit 761e06b

Browse files
javacheFacebook Github Bot
authored andcommitted
Don't receive touches in RCTRootView
Reviewed By: mmmulani, majak Differential Revision: D4104319 fbshipit-source-id: 70731b72e087710ccbc32024a596583640b94d04
1 parent 68aeffe commit 761e06b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

React/Base/RCTRootView.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ extern NSString *const RCTContentDidAppearNotification;
135135
*/
136136
- (void)cancelTouches;
137137

138+
/**
139+
* When set, any touches on the RCTRootView that are not matched up to any of the child
140+
* views will be passed to siblings of the RCTRootView. See -[UIView hitTest:withEvent:]
141+
* for details on iOS hit testing.
142+
*
143+
* Enable this to support a semi-transparent RN view that occupies the whole screen but
144+
* has visible content below it that the user can interact with.
145+
*
146+
* The default value is NO.
147+
*/
148+
@property (nonatomic, assign) BOOL passThroughTouches;
149+
138150
/**
139151
* Timings for hiding the loading view after the content has loaded. Both of
140152
* these values default to 0.25 seconds.

React/Base/RCTRootView.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ @interface RCTRootContentView : RCTView <RCTInvalidating>
4040

4141
@property (nonatomic, readonly) BOOL contentHasAppeared;
4242
@property (nonatomic, readonly, strong) RCTTouchHandler *touchHandler;
43+
@property (nonatomic, assign) BOOL passThroughTouches;
4344

4445
- (instancetype)initWithFrame:(CGRect)frame
4546
bridge:(RCTBridge *)bridge
@@ -125,6 +126,16 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor
125126
_contentView.backgroundColor = backgroundColor;
126127
}
127128

129+
- (BOOL)passThroughTouches
130+
{
131+
return _contentView.passThroughTouches;
132+
}
133+
134+
- (void)setPassThroughTouches:(BOOL)passThroughTouches
135+
{
136+
_contentView.passThroughTouches = passThroughTouches;
137+
}
138+
128139
- (UIViewController *)reactViewController
129140
{
130141
return _reactViewController ?: [super reactViewController];
@@ -259,6 +270,16 @@ - (void)layoutSubviews
259270
};
260271
}
261272

273+
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
274+
{
275+
// The root view itself should never receive touches
276+
UIView *hitView = [super hitTest:point withEvent:event];
277+
if (self.passThroughTouches && hitView == self) {
278+
return nil;
279+
}
280+
return hitView;
281+
}
282+
262283
- (void)setAppProperties:(NSDictionary *)appProperties
263284
{
264285
RCTAssertMainQueue();
@@ -382,6 +403,16 @@ - (UIColor *)backgroundColor
382403
return _backgroundColor;
383404
}
384405

406+
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
407+
{
408+
// The root content view itself should never receive touches
409+
UIView *hitView = [super hitTest:point withEvent:event];
410+
if (_passThroughTouches && hitView == self) {
411+
return nil;
412+
}
413+
return hitView;
414+
}
415+
385416
- (void)invalidate
386417
{
387418
if (self.userInteractionEnabled) {

0 commit comments

Comments
 (0)