-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMUActionSheet.m
executable file
·249 lines (201 loc) · 11.4 KB
/
MUActionSheet.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
#import "MUActionSheet.h"
static const CGFloat mRowHeight = 48.0f;
static const CGFloat mRowLineHeight = 0.5f;
static const CGFloat mSeparatorHeight = 6.0f;
static const CGFloat mTitleFontSize = 13.0f;
static const CGFloat mButtonTitleFontSize = 18.0f;
static const NSTimeInterval mAnimateDuration = 0.3f;
@interface MUActionSheet ()
/** block回调 */
@property (copy, nonatomic) MUActionSheetBlock actionSheetBlock;
/** 背景图片 */
@property (strong, nonatomic) UIView *backgroundView;
/** 弹出视图 */
@property (strong, nonatomic) UIView *actionSheetView;
/**
* 收起视图
*/
- (void)dismiss;
/**
* 通过颜色生成图片
*/
- (UIImage *)imageWithColor:(UIColor *)color;
@end
@implementation MUActionSheet
- (instancetype)initWithFrame:(CGRect)frame
{
return [self initWithTitle:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil handler:nil];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return [self initWithTitle:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil handler:nil];
}
- (instancetype)initWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles handler:(MUActionSheetBlock)actionSheetBlock
{
self = [super initWithFrame:CGRectZero];
if (self)
{
self.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_actionSheetBlock = actionSheetBlock;
CGFloat actionSheetHeight = 0;
_backgroundView = [[UIView alloc] initWithFrame:self.frame];
_backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
_backgroundView.alpha = 0;
[self addSubview:_backgroundView];
_actionSheetView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 0)];
_actionSheetView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
_actionSheetView.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
[self addSubview:_actionSheetView];
UIImage *normalImage = [self imageWithColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0f]];
UIImage *highlightedImage = [self imageWithColor:[UIColor colorWithRed:242.0f/255.0f green:242.0f/255.0f blue:242.0f/255.0f alpha:1.0f]];
if (title && title.length > 0)
{
actionSheetHeight += mRowLineHeight;
CGFloat titleHeight = ceil([title boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:mTitleFontSize]} context:nil].size.height) + 15*2;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, actionSheetHeight, self.frame.size.width, titleHeight)];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
titleLabel.text = title;
titleLabel.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0f];
titleLabel.textColor = [UIColor colorWithRed:135.0f/255.0f green:135.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:mTitleFontSize];
titleLabel.numberOfLines = 0;
[_actionSheetView addSubview:titleLabel];
actionSheetHeight += titleHeight;
}
if (destructiveButtonTitle && destructiveButtonTitle.length > 0)
{
actionSheetHeight += mRowLineHeight;
UIButton *destructiveButton = [UIButton buttonWithType:UIButtonTypeCustom];
destructiveButton.frame = CGRectMake(0, actionSheetHeight, self.frame.size.width, mRowHeight);
destructiveButton.autoresizingMask = UIViewAutoresizingFlexibleWidth;
destructiveButton.tag = -1;
destructiveButton.titleLabel.font = [UIFont systemFontOfSize:mButtonTitleFontSize];
[destructiveButton setTitle:destructiveButtonTitle forState:UIControlStateNormal];
[destructiveButton setTitleColor:[UIColor colorWithRed:230.0f/255.0f green:66.0f/255.0f blue:66.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[destructiveButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[destructiveButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[destructiveButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_actionSheetView addSubview:destructiveButton];
actionSheetHeight += mRowHeight;
}
if (otherButtonTitles && [otherButtonTitles count] > 0)
{
for (int i = 0; i < otherButtonTitles.count; i++)
{
actionSheetHeight += mRowLineHeight;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, actionSheetHeight, self.frame.size.width, mRowHeight);
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
button.tag = i+1;
button.titleLabel.font = [UIFont systemFontOfSize:mButtonTitleFontSize];
[button setTitle:otherButtonTitles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:64.0f/255.0f green:64.0f/255.0f blue:64.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_actionSheetView addSubview:button];
actionSheetHeight += mRowHeight;
}
}
if (cancelButtonTitle && cancelButtonTitle.length > 0)
{
actionSheetHeight += mSeparatorHeight;
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame = CGRectMake(0, actionSheetHeight, self.frame.size.width, mRowHeight);
cancelButton.autoresizingMask = UIViewAutoresizingFlexibleWidth;
cancelButton.tag = 0;
cancelButton.titleLabel.font = [UIFont systemFontOfSize:mButtonTitleFontSize];
[cancelButton setTitle:cancelButtonTitle ?: @"取消" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor colorWithRed:64.0f/255.0f green:64.0f/255.0f blue:64.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[cancelButton setBackgroundImage:normalImage forState:UIControlStateNormal];
[cancelButton setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
[cancelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[_actionSheetView addSubview:cancelButton];
actionSheetHeight += mRowHeight;
}
_actionSheetView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, actionSheetHeight);
}
return self;
}
+ (instancetype)actionSheetWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles handler:(MUActionSheetBlock)actionSheetBlock
{
return [[self alloc] initWithTitle:title cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles handler:actionSheetBlock];
}
+ (void)showActionSheetWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles handler:(MUActionSheetBlock)actionSheetBlock
{
MUActionSheet *lpActionSheet = [self actionSheetWithTitle:title cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitles handler:actionSheetBlock];
[lpActionSheet show];
}
- (void)show
{
// 在主线程中处理,否则在viewDidLoad方法中直接调用,会先加本视图,后加控制器的视图到UIWindow上,导致本视图无法显示出来,这样处理后便会优先加控制器的视图到UIWindow上
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
for (UIWindow *window in frontToBackWindows)
{
BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
BOOL windowIsVisible = !window.hidden && window.alpha > 0;
BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;
if(windowOnMainScreen && windowIsVisible && windowLevelNormal)
{
[window addSubview:self];
break;
}
}
[UIView animateWithDuration:mAnimateDuration delay:0 usingSpringWithDamping:0.7f initialSpringVelocity:0.7f options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.backgroundView.alpha = 1.0f;
self.actionSheetView.frame = CGRectMake(0, self.frame.size.height-self.actionSheetView.frame.size.height, self.frame.size.width, self.actionSheetView.frame.size.height);
} completion:nil];
}];
}
- (void)dismiss
{
[UIView animateWithDuration:mAnimateDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.backgroundView.alpha = 0.0f;
self.actionSheetView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.actionSheetView.frame.size.height);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.backgroundView];
if (!CGRectContainsPoint(self.actionSheetView.frame, point))
{
if (self.actionSheetBlock)
{
self.actionSheetBlock(self, 0);
}
[self dismiss];
}
}
- (void)buttonClicked:(UIButton *)button
{
if (self.actionSheetBlock)
{
self.actionSheetBlock(self, button.tag);
}
[self dismiss];
}
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)dealloc
{
#ifdef DEBUG
NSLog(@"LPActionSheet dealloc");
#endif
}
@end