This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIViewController+MJPopupViewController.m
executable file
·376 lines (321 loc) · 14.7 KB
/
UIViewController+MJPopupViewController.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
//
// UIViewController+MJPopupViewController.m
// MJModalViewController
//
// Created by Martin Juhasz on 11.05.12.
// Copyright (c) 2012 martinjuhasz.de. All rights reserved.
//
#import "UIViewController+MJPopupViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "MJPopupBackgroundView.h"
#import <objc/runtime.h>
#define kPopupModalAnimationDuration 0.35
#define kMJPopupViewController @"kMJPopupViewController"
#define kMJPopupBackgroundView @"kMJPopupBackgroundView"
#define kMJSourceViewTag 23941
#define kMJPopupViewTag 23942
#define kMJOverlayViewTag 23945
@interface UIViewController (MJPopupViewControllerPrivate)
- (UIView*)topView;
- (void)presentPopupView:(UIView*)popupView;
@end
static NSString *MJPopupViewDismissedKey = @"MJPopupViewDismissed";
////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public
@implementation UIViewController (MJPopupViewController)
static void * const keypath = (void*)&keypath;
- (UIViewController*)mj_popupViewController {
return objc_getAssociatedObject(self, kMJPopupViewController);
}
- (void)setMj_popupViewController:(UIViewController *)mj_popupViewController {
objc_setAssociatedObject(self, kMJPopupViewController, mj_popupViewController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (MJPopupBackgroundView*)mj_popupBackgroundView {
return objc_getAssociatedObject(self, kMJPopupBackgroundView);
}
- (void)setMj_popupBackgroundView:(MJPopupBackgroundView *)mj_popupBackgroundView {
objc_setAssociatedObject(self, kMJPopupBackgroundView, mj_popupBackgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed
{
self.mj_popupViewController = popupViewController;
[self presentPopupView:popupViewController.view animationType:animationType dismissed:dismissed];
}
- (void)presentPopupViewController:(UIViewController*)popupViewController animationType:(MJPopupViewAnimation)animationType
{
[self presentPopupViewController:popupViewController animationType:animationType dismissed:nil];
}
- (void)dismissPopupViewControllerWithanimationType:(MJPopupViewAnimation)animationType
{
UIView *sourceView = [self topView];
UIView *popupView = [sourceView viewWithTag:kMJPopupViewTag];
UIView *overlayView = [sourceView viewWithTag:kMJOverlayViewTag];
switch (animationType) {
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideBottomBottom:
case MJPopupViewAnimationSlideTopTop:
case MJPopupViewAnimationSlideTopBottom:
case MJPopupViewAnimationSlideLeftLeft:
case MJPopupViewAnimationSlideLeftRight:
case MJPopupViewAnimationSlideRightLeft:
case MJPopupViewAnimationSlideRightRight:
[self slideViewOut:popupView sourceView:sourceView overlayView:overlayView withAnimationType:animationType];
break;
default:
[self fadeViewOut:popupView sourceView:sourceView overlayView:overlayView];
break;
}
self.mj_popupViewController = nil;
}
////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark View Handling
- (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)animationType
{
[self presentPopupView:popupView animationType:animationType dismissed:nil];
}
- (void)presentPopupView:(UIView*)popupView animationType:(MJPopupViewAnimation)animationType dismissed:(void(^)(void))dismissed
{
UIView *sourceView = [self topView];
sourceView.tag = kMJSourceViewTag;
popupView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
popupView.tag = kMJPopupViewTag;
// check if source view controller is not in destination
if ([sourceView.subviews containsObject:popupView]) return;
// customize popupView
popupView.layer.shadowPath = [UIBezierPath bezierPathWithRect:popupView.bounds].CGPath;
popupView.layer.masksToBounds = NO;
popupView.layer.shadowOffset = CGSizeMake(5, 5);
popupView.layer.shadowRadius = 5;
popupView.layer.shadowOpacity = 0.5;
popupView.layer.shouldRasterize = YES;
popupView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
// Add semi overlay
UIView *overlayView = [[UIView alloc] initWithFrame:sourceView.bounds];
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayView.tag = kMJOverlayViewTag;
overlayView.backgroundColor = [UIColor clearColor];
// BackgroundView
self.mj_popupBackgroundView = [[MJPopupBackgroundView alloc] initWithFrame:sourceView.bounds];
self.mj_popupBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mj_popupBackgroundView.backgroundColor = [UIColor clearColor];
self.mj_popupBackgroundView.alpha = 0.0f;
[overlayView addSubview:self.mj_popupBackgroundView];
// Make the Background Clickable
UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
dismissButton.backgroundColor = [UIColor clearColor];
dismissButton.frame = sourceView.bounds;
[overlayView addSubview:dismissButton];
popupView.alpha = 0.0f;
[overlayView addSubview:popupView];
[sourceView addSubview:overlayView];
[dismissButton addTarget:self action:@selector(dismissPopupViewControllerWithanimation:) forControlEvents:UIControlEventTouchUpInside];
switch (animationType) {
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideBottomBottom:
case MJPopupViewAnimationSlideTopTop:
case MJPopupViewAnimationSlideTopBottom:
case MJPopupViewAnimationSlideLeftLeft:
case MJPopupViewAnimationSlideLeftRight:
case MJPopupViewAnimationSlideRightLeft:
case MJPopupViewAnimationSlideRightRight:
dismissButton.tag = animationType;
[self slideViewIn:popupView sourceView:sourceView overlayView:overlayView withAnimationType:animationType];
break;
default:
dismissButton.tag = MJPopupViewAnimationFade;
[self fadeViewIn:popupView sourceView:sourceView overlayView:overlayView];
break;
}
[self setDismissedCallback:dismissed];
}
-(UIView*)topView {
UIViewController *recentView = self;
while (recentView.parentViewController != nil) {
recentView = recentView.parentViewController;
}
return recentView.view;
}
- (void)dismissPopupViewControllerWithanimation:(id)sender
{
if ([sender isKindOfClass:[UIButton class]]) {
UIButton* dismissButton = sender;
switch (dismissButton.tag) {
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideBottomBottom:
case MJPopupViewAnimationSlideTopTop:
case MJPopupViewAnimationSlideTopBottom:
case MJPopupViewAnimationSlideLeftLeft:
case MJPopupViewAnimationSlideLeftRight:
case MJPopupViewAnimationSlideRightLeft:
case MJPopupViewAnimationSlideRightRight:
[self dismissPopupViewControllerWithanimationType:dismissButton.tag];
break;
default:
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
break;
}
} else {
[self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}
}
//////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Animations
#pragma mark --- Slide
- (void)slideViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView withAnimationType:(MJPopupViewAnimation)animationType
{
// Generating Start and Stop Positions
CGSize sourceSize = sourceView.bounds.size;
CGSize popupSize = popupView.bounds.size;
CGRect popupStartRect;
switch (animationType) {
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideBottomBottom:
popupStartRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
sourceSize.height,
popupSize.width,
popupSize.height);
break;
case MJPopupViewAnimationSlideLeftLeft:
case MJPopupViewAnimationSlideLeftRight:
popupStartRect = CGRectMake(-sourceSize.width,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.height);
break;
case MJPopupViewAnimationSlideTopTop:
case MJPopupViewAnimationSlideTopBottom:
popupStartRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
-popupSize.height,
popupSize.width,
popupSize.height);
break;
default:
popupStartRect = CGRectMake(sourceSize.width,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.height);
break;
}
CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.height);
// Set starting properties
popupView.frame = popupStartRect;
popupView.alpha = 1.0f;
[UIView animateWithDuration:kPopupModalAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
[self.mj_popupViewController viewWillAppear:NO];
self.mj_popupBackgroundView.alpha = 1.0f;
popupView.frame = popupEndRect;
} completion:^(BOOL finished) {
[self.mj_popupViewController viewDidAppear:NO];
}];
}
- (void)slideViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView withAnimationType:(MJPopupViewAnimation)animationType
{
// Generating Start and Stop Positions
CGSize sourceSize = sourceView.bounds.size;
CGSize popupSize = popupView.bounds.size;
CGRect popupEndRect;
switch (animationType) {
case MJPopupViewAnimationSlideBottomTop:
case MJPopupViewAnimationSlideTopTop:
popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
-popupSize.height,
popupSize.width,
popupSize.height);
break;
case MJPopupViewAnimationSlideBottomBottom:
case MJPopupViewAnimationSlideTopBottom:
popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
sourceSize.height,
popupSize.width,
popupSize.height);
break;
case MJPopupViewAnimationSlideLeftRight:
case MJPopupViewAnimationSlideRightRight:
popupEndRect = CGRectMake(sourceSize.width,
popupView.frame.origin.y,
popupSize.width,
popupSize.height);
break;
default:
popupEndRect = CGRectMake(-popupSize.width,
popupView.frame.origin.y,
popupSize.width,
popupSize.height);
break;
}
[UIView animateWithDuration:kPopupModalAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.mj_popupViewController viewWillDisappear:NO];
popupView.frame = popupEndRect;
self.mj_popupBackgroundView.alpha = 0.0f;
} completion:^(BOOL finished) {
[popupView removeFromSuperview];
[overlayView removeFromSuperview];
[self.mj_popupViewController viewDidDisappear:NO];
self.mj_popupViewController = nil;
id dismissed = [self dismissedCallback];
if (dismissed != nil)
{
((void(^)(void))dismissed)();
[self setDismissedCallback:nil];
}
}];
}
#pragma mark --- Fade
- (void)fadeViewIn:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView
{
// Generating Start and Stop Positions
CGSize sourceSize = sourceView.bounds.size;
CGSize popupSize = popupView.bounds.size;
CGRect popupEndRect = CGRectMake((sourceSize.width - popupSize.width) / 2,
(sourceSize.height - popupSize.height) / 2,
popupSize.width,
popupSize.height);
// Set starting properties
popupView.frame = popupEndRect;
popupView.alpha = 0.0f;
[UIView animateWithDuration:kPopupModalAnimationDuration animations:^{
[self.mj_popupViewController viewWillAppear:NO];
self.mj_popupBackgroundView.alpha = 0.5f;
popupView.alpha = 1.0f;
} completion:^(BOOL finished) {
[self.mj_popupViewController viewDidAppear:NO];
}];
}
- (void)fadeViewOut:(UIView*)popupView sourceView:(UIView*)sourceView overlayView:(UIView*)overlayView
{
[UIView animateWithDuration:kPopupModalAnimationDuration animations:^{
[self.mj_popupViewController viewWillDisappear:NO];
self.mj_popupBackgroundView.alpha = 0.0f;
popupView.alpha = 0.0f;
} completion:^(BOOL finished) {
[popupView removeFromSuperview];
[overlayView removeFromSuperview];
[self.mj_popupViewController viewDidDisappear:NO];
self.mj_popupViewController = nil;
id dismissed = [self dismissedCallback];
if (dismissed != nil)
{
((void(^)(void))dismissed)();
[self setDismissedCallback:nil];
}
}];
}
#pragma mark -
#pragma mark Category Accessors
#pragma mark --- Dismissed
- (void)setDismissedCallback:(void(^)(void))dismissed
{
objc_setAssociatedObject(self, &MJPopupViewDismissedKey, dismissed, OBJC_ASSOCIATION_RETAIN);
}
- (void(^)(void))dismissedCallback
{
return objc_getAssociatedObject(self, &MJPopupViewDismissedKey);
}
@end