forked from kgn/KGModal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KGModal.m
417 lines (342 loc) · 15.1 KB
/
KGModal.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//
// KGModal.m
// KGModal
//
// Created by David Keegan on 10/5/12.
// Copyright (c) 2012 David Keegan. All rights reserved.
//
#import "KGModal.h"
#import <QuartzCore/QuartzCore.h>
CGFloat const kFadeInAnimationDuration = 0.3;
CGFloat const kTransformPart1AnimationDuration = 0.2;
CGFloat const kTransformPart2AnimationDuration = 0.1;
NSString *const KGModalGradientViewTapped = @"KGModalGradientViewTapped";
CGFloat const kCloseButtonPadding = 10;
@interface KGModalGradientView : UIView
@end
@interface KGModalContainerView : UIView
@property (weak, nonatomic) CALayer *styleLayer;
@property (strong, nonatomic) UIColor *modalBackgroundColor;
@end
@interface KGModalCloseButton : UIButton
@end
@interface KGModalViewController : UIViewController
@property (weak, nonatomic) KGModalGradientView *styleView;
@end
@interface KGModal()
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *contentViewController;
@property (weak, nonatomic) KGModalViewController *viewController;
@property (weak, nonatomic) KGModalContainerView *containerView;
@property (weak, nonatomic) KGModalCloseButton *closeButton;
@property (weak, nonatomic) UIView *contentView;
@end
@implementation KGModal
+ (instancetype)sharedInstance{
static id sharedInstance;
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedInstance = [[[self class] alloc] init];
});
return sharedInstance;
}
- (instancetype)init{
if(!(self = [super init])){
return nil;
}
self.shouldRotate = YES;
self.tapOutsideToDismiss = YES;
self.animateWhenDismissed = YES;
self.showCloseButton = YES;
self.modalBackgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
self.tapOutsidePadding = 17.;
return self;
}
- (void)setTapOutsidePadding:(CGFloat)tapOutsidePadding {
if (tapOutsidePadding < 0) {
// Negative values are not allowed
tapOutsidePadding = 0;
}
_tapOutsidePadding = tapOutsidePadding;
}
- (void)setShowCloseButton:(BOOL)showCloseButton{
if(_showCloseButton != showCloseButton){
_showCloseButton = showCloseButton;
[self.closeButton setHidden:!self.showCloseButton];
}
}
- (void)showWithContentView:(UIView *)contentView{
[self showWithContentView:contentView andAnimated:YES];
}
- (void)showWithContentViewController:(UIViewController *)contentViewController{
[self showWithContentViewController:contentViewController andAnimated:YES];
}
- (void)showWithContentViewController:(UIViewController *)contentViewController andAnimated:(BOOL)animated{
self.contentViewController = contentViewController;
[self showWithContentView:contentViewController.view andAnimated:YES];
}
- (void)showWithContentView:(UIView *)contentView andAnimated:(BOOL)animated {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.window.opaque = NO;
KGModalViewController *viewController = [[KGModalViewController alloc] init];
self.window.rootViewController = viewController;
self.viewController = viewController;
CGFloat padding = self.tapOutsidePadding;
CGRect containerViewRect = CGRectInset(contentView.frame, -padding, -padding);
containerViewRect.origin.x = containerViewRect.origin.y = 0;
KGModalContainerView *containerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect];
containerView.center = CGPointMake(self.window.bounds.size.width / 2. , self.window.bounds.size.height / 2.);
containerView.modalBackgroundColor = self.modalBackgroundColor;
containerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
containerView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
contentView.center = CGPointMake(containerView.bounds.size.width/2., containerView.bounds.size.height/2.);
contentView.frame = (CGRect){padding, padding, contentView.bounds.size};
[containerView addSubview:contentView];
[viewController.view addSubview:containerView];
self.containerView = containerView;
KGModalCloseButton *closeButton = [[KGModalCloseButton alloc] init];
[closeButton addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
[closeButton setHidden:!self.showCloseButton];
[self.window addSubview:closeButton];
CGFloat x = (self.window.bounds.size.width - closeButton.bounds.size.width) - kCloseButtonPadding;
CGFloat y = kCloseButtonPadding + 20; // 20 pts for status bar (iOS7 and before)
closeButton.frame = CGRectMake(x, y, closeButton.bounds.size.width, closeButton.bounds.size.height);
self.closeButton = closeButton;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tapCloseAction:)
name:KGModalGradientViewTapped object:nil];
// The window has to be un-hidden on the main thread
// This will cause the window to display
dispatch_async(dispatch_get_main_queue(), ^{
[self.window makeKeyAndVisible];
[self.window bringSubviewToFront:self.closeButton];
if(animated){
viewController.styleView.alpha = 0;
[UIView animateWithDuration:kFadeInAnimationDuration animations:^{
viewController.styleView.alpha = 1;
}];
containerView.alpha = 0;
containerView.layer.shouldRasterize = YES;
containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
[UIView animateWithDuration:kTransformPart1AnimationDuration animations:^{
containerView.alpha = 1;
containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:kTransformPart2AnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
containerView.alpha = 1;
containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
} completion:^(BOOL finished2) {
containerView.layer.shouldRasterize = NO;
}];
}];
}
});
}
- (void)closeAction:(id)sender{
[self hideAnimated:self.animateWhenDismissed];
}
- (void)tapCloseAction:(id)sender{
if(self.tapOutsideToDismiss){
[self hideAnimated:self.animateWhenDismissed];
}
}
- (void)hide{
[self hideAnimated:YES];
}
- (void)hideWithCompletionBlock:(void(^)())completion{
[self hideAnimated:YES withCompletionBlock:completion];
}
- (void)hideAnimated:(BOOL)animated{
[self hideAnimated:animated withCompletionBlock:nil];
}
- (void)hideAnimated:(BOOL)animated withCompletionBlock:(void(^)())completion{
if(!animated){
[self cleanup];
if (self.modalDidFinishDismissing) {
self.modalDidFinishDismissing();
}
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:kFadeInAnimationDuration animations:^{
self.viewController.styleView.alpha = 0;
}];
self.containerView.layer.shouldRasterize = YES;
[UIView animateWithDuration:kTransformPart2AnimationDuration animations:^{
self.containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished){
[UIView animateWithDuration:kTransformPart1AnimationDuration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.containerView.alpha = 0;
self.containerView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
} completion:^(BOOL finished2){
[self cleanup];
if(completion){
completion();
}
if (self.modalDidFinishDismissing) {
self.modalDidFinishDismissing();
}
}];
}];
});
}
- (void)cleanup{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.containerView removeFromSuperview];
[[[[UIApplication sharedApplication] delegate] window] makeKeyWindow];
[self.window removeFromSuperview];
self.contentViewController = nil;
self.window = nil;
}
- (void)setModalBackgroundColor:(UIColor *)modalBackgroundColor{
if(_modalBackgroundColor != modalBackgroundColor){
_modalBackgroundColor = modalBackgroundColor;
self.containerView.modalBackgroundColor = modalBackgroundColor;
}
}
- (void)dealloc{
[self cleanup];
}
@end
@implementation KGModalViewController
- (void)loadView{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
KGModalGradientView *styleView = [[KGModalGradientView alloc] initWithFrame:self.view.bounds];
styleView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
styleView.opaque = NO;
[self.view addSubview:styleView];
self.styleView = styleView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return [[KGModal sharedInstance] shouldRotate];
}
@end
@implementation KGModalContainerView
- (instancetype)initWithFrame:(CGRect)frame{
if(!(self = [super initWithFrame:frame])){
return nil;
}
CALayer *styleLayer = [[CALayer alloc] init];
styleLayer.cornerRadius = 4;
styleLayer.shadowColor= [[UIColor blackColor] CGColor];
styleLayer.shadowOffset = CGSizeMake(0, 0);
styleLayer.shadowOpacity = 0.5;
styleLayer.borderWidth = 1;
styleLayer.borderColor = [[UIColor whiteColor] CGColor];
styleLayer.frame = CGRectInset(self.bounds, 12, 12);
[self.layer addSublayer:styleLayer];
self.styleLayer = styleLayer;
return self;
}
- (void)setModalBackgroundColor:(UIColor *)modalBackgroundColor{
if(_modalBackgroundColor != modalBackgroundColor){
_modalBackgroundColor = modalBackgroundColor;
self.styleLayer.backgroundColor = [modalBackgroundColor CGColor];
}
}
@end
@implementation KGModalGradientView
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[[NSNotificationCenter defaultCenter] postNotificationName:KGModalGradientViewTapped object:nil];
}
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
if([[KGModal sharedInstance] backgroundDisplayStyle] == KGModalBackgroundDisplayStyleSolid){
[[UIColor colorWithWhite:0 alpha:0.55] set];
CGContextFillRect(context, self.bounds);
}else{
CGContextSaveGState(context);
size_t gradLocationsNum = 2;
CGFloat gradLocations[2] = {0.0f, 1.0f};
CGFloat gradColors[8] = {0, 0, 0, 0.3, 0, 0, 0, 0.8};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
CGColorSpaceRelease(colorSpace), colorSpace = NULL;
CGPoint gradCenter= CGPointMake(round(CGRectGetMidX(self.bounds)), round(CGRectGetMidY(self.bounds)));
CGFloat gradRadius = MAX(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
CGContextDrawRadialGradient(context, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient), gradient = NULL;
CGContextRestoreGState(context);
}
}
@end
@implementation KGModalCloseButton
- (instancetype)init{
if(!(self = [super initWithFrame:(CGRect){0, 0, 32, 32}])){
return nil;
}
static UIImage *closeButtonImage;
static dispatch_once_t once;
dispatch_once(&once, ^{
closeButtonImage = [self closeButtonImage];
});
[self setBackgroundImage:closeButtonImage forState:UIControlStateNormal];
return self;
}
- (UIImage *)closeButtonImage{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor *topGradient = [UIColor colorWithRed:0.21 green:0.21 blue:0.21 alpha:0.9];
UIColor *bottomGradient = [UIColor colorWithRed:0.03 green:0.03 blue:0.03 alpha:0.9];
//// Gradient Declarations
NSArray *gradientColors = @[(id)topGradient.CGColor,
(id)bottomGradient.CGColor];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// Shadow Declarations
CGColorRef shadow = [UIColor blackColor].CGColor;
CGSize shadowOffset = CGSizeMake(0, 1);
CGFloat shadowBlurRadius = 3;
CGColorRef shadow2 = [UIColor blackColor].CGColor;
CGSize shadow2Offset = CGSizeMake(0, 1);
CGFloat shadow2BlurRadius = 0;
//// Oval Drawing
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(4, 3, 24, 24)];
CGContextSaveGState(context);
[ovalPath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(16, 3), CGPointMake(16, 27), 0);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow);
[[UIColor whiteColor] setStroke];
ovalPath.lineWidth = 2;
[ovalPath stroke];
CGContextRestoreGState(context);
//// Bezier Drawing
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(22.36, 11.46)];
[bezierPath addLineToPoint:CGPointMake(18.83, 15)];
[bezierPath addLineToPoint:CGPointMake(22.36, 18.54)];
[bezierPath addLineToPoint:CGPointMake(19.54, 21.36)];
[bezierPath addLineToPoint:CGPointMake(16, 17.83)];
[bezierPath addLineToPoint:CGPointMake(12.46, 21.36)];
[bezierPath addLineToPoint:CGPointMake(9.64, 18.54)];
[bezierPath addLineToPoint:CGPointMake(13.17, 15)];
[bezierPath addLineToPoint:CGPointMake(9.64, 11.46)];
[bezierPath addLineToPoint:CGPointMake(12.46, 8.64)];
[bezierPath addLineToPoint:CGPointMake(16, 12.17)];
[bezierPath addLineToPoint:CGPointMake(19.54, 8.64)];
[bezierPath addLineToPoint:CGPointMake(22.36, 11.46)];
[bezierPath closePath];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, shadow2);
[[UIColor whiteColor] setFill];
[bezierPath fill];
CGContextRestoreGState(context);
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end