forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCCLightBox.m
executable file
·242 lines (204 loc) · 8.18 KB
/
RCCLightBox.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#import "RCCLightBox.h"
#import "RCCManager.h"
#import <React/RCTRootView.h>
#import <React/RCTRootViewDelegate.h>
#import <React/RCTConvert.h>
#import <React/RCTUtils.h>
#import "RCTHelpers.h"
#import <objc/runtime.h>
const NSInteger kLightBoxTag = 0x101010;
@interface RCCLightBoxView : UIView<UIGestureRecognizerDelegate>
@property (nonatomic, strong) RCTRootView *reactView;
@property (nonatomic, strong) UIVisualEffectView *visualEffectView;
@property (nonatomic, strong) UIView *overlayColorView;
@property (nonatomic, strong) NSDictionary *params;
@property (nonatomic) BOOL yellowBoxRemoved;
@end
@implementation RCCLightBoxView
-(instancetype)initWithFrame:(CGRect)frame params:(NSDictionary*)params
{
self = [super initWithFrame:frame];
if (self)
{
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.params = params;
self.yellowBoxRemoved = NO;
NSDictionary *passProps = self.params[@"passProps"];
NSDictionary *style = self.params[@"style"];
if (self.params != nil && style != nil)
{
if (style[@"backgroundBlur"] != nil && ![style[@"backgroundBlur"] isEqualToString:@"none"])
{
self.visualEffectView = [[UIVisualEffectView alloc] init];
self.visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.visualEffectView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
[self addSubview:self.visualEffectView];
}
if (style[@"backgroundColor"] != nil)
{
UIColor *backgroundColor = [RCTConvert UIColor:style[@"backgroundColor"]];
if (backgroundColor != nil)
{
self.overlayColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
self.overlayColorView.backgroundColor = backgroundColor;
self.overlayColorView.alpha = 0;
[self addSubview:self.overlayColorView];
}
}
if (style[@"tapBackgroundToDismiss"] != nil && [RCTConvert BOOL:style[@"tapBackgroundToDismiss"]])
{
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissAnimated)];
singleTap.delegate = self;
[self addGestureRecognizer:singleTap];
}
}
self.reactView = [[RCTRootView alloc] initWithBridge:[[RCCManager sharedInstance] getBridge] moduleName:self.params[@"component"] initialProperties:passProps];
self.reactView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
self.reactView.backgroundColor = [UIColor clearColor];
self.reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
self.reactView.center = self.center;
[self addSubview:self.reactView];
[self.reactView.contentView.layer addObserver:self forKeyPath:@"frame" options:0 context:nil];
[self.reactView.contentView.layer addObserver:self forKeyPath:@"bounds" options:0 context:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onRNReload) name:RCTJavaScriptWillStartLoadingNotification object:nil];
}
return self;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return ![touch.view isDescendantOfView:self.reactView];
}
-(void)layoutSubviews
{
[super layoutSubviews];
if(!self.yellowBoxRemoved)
{
self.yellowBoxRemoved = [RCTHelpers removeYellowBox:self.reactView];
}
}
-(void)removeAllObservers
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.reactView.contentView.layer removeObserver:self forKeyPath:@"frame" context:nil];
[self.reactView.contentView.layer removeObserver:self forKeyPath:@"bounds" context:NULL];
}
-(void)dealloc
{
[self removeAllObservers];
}
-(void)onRNReload
{
[self removeAllObservers];
[self removeFromSuperview];
self.reactView = nil;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
CGSize frameSize = CGSizeZero;
if ([object isKindOfClass:[CALayer class]])
frameSize = ((CALayer*)object).frame.size;
if ([object isKindOfClass:[UIView class]])
frameSize = ((UIView*)object).frame.size;
if (!CGSizeEqualToSize(frameSize, self.reactView.frame.size))
{
self.reactView.frame = CGRectMake((self.frame.size.width - frameSize.width) * 0.5, (self.frame.size.height - frameSize.height) * 0.5, frameSize.width, frameSize.height);
}
}
-(UIBlurEffect*)blurEfectForCurrentStyle
{
NSDictionary *style = self.params[@"style"];
NSString *backgroundBlur = style[@"backgroundBlur"];
if ([backgroundBlur isEqualToString:@"none"])
{
return nil;
}
UIBlurEffectStyle blurEffectStyle = UIBlurEffectStyleDark;
if ([backgroundBlur isEqualToString:@"light"])
blurEffectStyle = UIBlurEffectStyleLight;
else if ([backgroundBlur isEqualToString:@"xlight"])
blurEffectStyle = UIBlurEffectStyleExtraLight;
else if ([backgroundBlur isEqualToString:@"dark"])
blurEffectStyle = UIBlurEffectStyleDark;
return [UIBlurEffect effectWithStyle:blurEffectStyle];
}
-(void)showAnimated
{
if (self.visualEffectView != nil || self.overlayColorView != nil)
{
[UIView animateWithDuration:0.3 animations:^()
{
if (self.visualEffectView != nil)
{
self.visualEffectView.effect = [self blurEfectForCurrentStyle];
}
if (self.overlayColorView != nil)
{
self.overlayColorView.alpha = 1;
}
}];
}
self.reactView.transform = CGAffineTransformMakeTranslation(0, 100);
self.reactView.alpha = 0;
[UIView animateWithDuration:0.6 delay:0.2 usingSpringWithDamping:0.65 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^()
{
self.reactView.transform = CGAffineTransformIdentity;
self.reactView.alpha = 1;
} completion:nil];
}
-(void)dismissAnimated
{
BOOL hasOverlayViews = (self.visualEffectView != nil || self.overlayColorView != nil);
[UIView animateWithDuration:0.2 animations:^()
{
self.reactView.transform = CGAffineTransformMakeTranslation(0, 80);
self.reactView.alpha = 0;
}
completion:^(BOOL finished)
{
if (!hasOverlayViews)
{
[self removeFromSuperview];
}
}];
if (hasOverlayViews)
{
[UIView animateWithDuration:0.25 delay:0.15 options:UIViewAnimationOptionCurveEaseOut animations:^()
{
if (self.visualEffectView != nil)
{
self.visualEffectView.effect = nil;
}
if (self.overlayColorView != nil)
{
self.overlayColorView.alpha = 0;
}
} completion:^(BOOL finished)
{
[self removeFromSuperview];
}];
}
}
@end
@implementation RCCLightBox
+(void)showWithParams:(NSDictionary*)params
{
UIViewController *viewController = RCTPresentedViewController();
if ([viewController.view viewWithTag:kLightBoxTag] != nil)
{
return;
}
RCCLightBoxView *lightBox = [[RCCLightBoxView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) params:params];
lightBox.tag = kLightBoxTag;
[viewController.view addSubview:lightBox];
[lightBox showAnimated];
}
+(void)dismiss
{
UIViewController *viewController = RCTPresentedViewController();
RCCLightBoxView *lightBox = [viewController.view viewWithTag:kLightBoxTag];
if (lightBox != nil)
{
[lightBox dismissAnimated];
}
}
@end