-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSWActionSheet.m
executable file
·192 lines (158 loc) · 5.4 KB
/
SWActionSheet.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
//
// Created by Petr Korolev on 11/08/14.
//
#import "SWActionSheet.h"
static UIWindow *SWActionSheetWindow = nil;
static const float delay = 0.f;
static const float duration = .3f;
static const enum UIViewAnimationOptions options = UIViewAnimationOptionCurveEaseIn;
@interface SWActionSheetVC : BaseViewController
@property (nonatomic, retain) SWActionSheet *actionSheet;
@end
@interface SWActionSheet ()
@property (nonatomic, assign) BOOL presented;
- (void)configureFrameForBounds:(CGRect)bounds;
- (void)showInContainerViewAnimated:(BOOL)animated;
@end
@implementation SWActionSheet
{
UIView *view;
UIView *_bgView;
}
- (void)dismissWithClickedButtonIndex:(int)i animated:(BOOL)animated
{
CGPoint fadeOutToPoint = CGPointMake(view.center.x,
self.center.y + CGRectGetHeight(view.frame));
// Window of app
//UIWindow *appWindow = [UIApplication sharedApplication].windows.firstObject;
// Actions
void (^actions)() = ^{
self.center = fadeOutToPoint;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.0f];
};
void (^completion)(BOOL) = ^(BOOL finished) {
// if (![appWindow isKeyWindow])
// [appWindow makeKeyAndVisible];
[SWActionSheet destroyWindow];
[self removeFromSuperview];
};
// Do actions animated or not
if (animated) {
[UIView animateWithDuration:duration delay:delay options:options animations:actions completion:completion];
} else {
actions();
completion(YES);
}
self.presented = NO;
}
+ (void)destroyWindow
{
if (SWActionSheetWindow)
{
[SWActionSheet actionSheetContainer].actionSheet = nil;
SWActionSheetWindow.hidden = YES;
if ([SWActionSheetWindow isKeyWindow])
[SWActionSheetWindow resignFirstResponder];
SWActionSheetWindow = nil;
}
}
+ (UIWindow *)window
{
return (SWActionSheetWindow ?: (SWActionSheetWindow = ({
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.windowLevel = UIWindowLevelAlert;
window.backgroundColor = [UIColor clearColor];
window.rootViewController = [SWActionSheetVC new];
window;
})));
}
+ (SWActionSheetVC *)actionSheetContainer
{
return (SWActionSheetVC *) [SWActionSheet window].rootViewController;
}
- (instancetype)initWithView:(UIView *)aView
{
if ((self = [super init]))
{
view = aView;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.0f];
_bgView = [UIView new];
_bgView.backgroundColor = [UIColor whiteColor];
[self addSubview:_bgView];
[self addSubview:view];
}
return self;
}
- (void)configureFrameForBounds:(CGRect)bounds
{
self.frame = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height + view.bounds.size.height);
view.frame = CGRectMake(view.bounds.origin.x, bounds.size.height, view.bounds.size.width, view.bounds.size.height);
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_bgView.frame = view.frame;
_bgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated
{
[self showInContainerView];
}
- (void)showInContainerView
{
// Make sheet window visible and active
UIWindow *sheetWindow = [SWActionSheet window];
if (![sheetWindow isKeyWindow])
[sheetWindow makeKeyAndVisible];
sheetWindow.hidden = NO;
// Put our ActionSheet in Container (it will be presented as soon as possible)
[SWActionSheet actionSheetContainer].actionSheet = self;
}
- (void)showInContainerViewAnimated:(BOOL)animated
{
CGPoint toPoint;
CGFloat y = self.center.y - CGRectGetHeight(view.frame);
toPoint = CGPointMake(self.center.x, y);
// Present actions
void (^animations)() = ^{
self.center = toPoint;
self.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5f];
};
// Present sheet
if (animated)
[UIView animateWithDuration:duration delay:delay options:options animations:animations completion:nil];
else
animations();
self.presented = YES;
}
@end
#pragma mark - SWActionSheet Container
@implementation SWActionSheetVC
- (void)setActionSheet:(SWActionSheet *)actionSheet
{
// Prevent processing one action sheet twice
if (_actionSheet == actionSheet)
return;
// Dissmiss previous action sheet if it presented
if (_actionSheet.presented)
[_actionSheet dismissWithClickedButtonIndex:0 animated:YES];
// Remember new action sheet
_actionSheet = actionSheet;
// Present new action sheet
[self presentActionSheetAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor clearColor];
[self presentActionSheetAnimated:YES];
}
- (void)presentActionSheetAnimated:(BOOL)animated
{
// New action sheet will be presented only when view controller will be loaded
if (_actionSheet && [self isViewLoaded] && !_actionSheet.presented)
{
[_actionSheet configureFrameForBounds:self.view.bounds];
_actionSheet.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_actionSheet];
[_actionSheet showInContainerViewAnimated:animated];
}
}
@end