-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIViewController+SemiModal.m
115 lines (93 loc) · 2.94 KB
/
UIViewController+SemiModal.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
#import "UIViewController+SemiModal.h"
#import <objc/runtime.h>
#import <sys/utsname.h>
#define kSheetModalViewController @"kSemiModalViewController"
#define kSheetModalView @"kSemiModalView"
// Presenting semimodal controller
@implementation UIViewController (SemiModal)
- (void)presentSheetViewController:(UIViewController *)vc animated:(BOOL)animated
{
if (! vc)
{
return;
}
objc_setAssociatedObject(self, kSheetModalViewController, vc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
UIView *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [[UIView alloc] initWithFrame:window.bounds];
view.backgroundColor = [UIColor clearColor];
[window addSubview:view];
objc_setAssociatedObject(self, kSheetModalView, view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
CGRect sheetFrame = vc.view.frame;
sheetFrame.origin.y = window.bounds.size.height;
vc.view.frame = sheetFrame;
[window addSubview:vc.view];
[vc viewWillAppear:animated];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
if (animated)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(sheetAnimationDidStop:)];
}
CGRect frame = vc.view.frame;
frame.origin.y = window.bounds.size.height - frame.size.height;
vc.view.frame = frame;
if (animated)
{
[UIView commitAnimations];
}
else
{
[self sheetAnimationDidStop:nil];
}
}
- (void)sheetAnimationDidStop:(id)context
{
UIViewController *vc = objc_getAssociatedObject(self, kSheetModalViewController);
if (vc != nil) {
[vc viewDidAppear:NO];
}
}
#pragma mark -
- (void)dismissSheetView
{
UIViewController *vc = objc_getAssociatedObject(self, kSheetModalViewController);
[self dismissSheetViewController:vc animated:YES];
}
- (void)dismissSheetViewController:(UIViewController *)vc animated:(BOOL)animated
{
[vc viewWillDisappear:animated];
if (animated)
{
[UIView animateWithDuration:0.3
animations:^(void) {
UIView *sheetView = objc_getAssociatedObject(self, kSheetModalView);
sheetView.backgroundColor = [UIColor clearColor];
CGRect frame = vc.view.frame;
frame.origin.y = self.view.bounds.size.height;
vc.view.frame = frame;
}
completion:^(BOOL finished)
{
if (finished)
{
[self dismissSheetViewControllerDidEndAnimated:animated];
}
}];
}
else
{
[self dismissSheetViewControllerDidEndAnimated:animated];
}
}
- (void)dismissSheetViewControllerDidEndAnimated:(BOOL)animated
{
UIViewController *vc = objc_getAssociatedObject(self, kSheetModalViewController);
[vc viewDidDisappear:animated];
UIView *sheetView = objc_getAssociatedObject(self, kSheetModalView);
[sheetView removeFromSuperview];
objc_setAssociatedObject(self, kSheetModalView, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[vc.view removeFromSuperview];
objc_setAssociatedObject(self, kSheetModalViewController, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end