forked from reddavis/RDActionSheet
/
RDActionSheet.m
351 lines (248 loc) · 12.5 KB
/
RDActionSheet.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
//
// RDActionSheet.m
// RDActionSheet v1.1.0
//
// Created by Red Davis on 12/01/2012.
// Copyright (c) 2012 Riot. All rights reserved.
//
#import "RDActionSheet.h"
#import <QuartzCore/QuartzCore.h>
@interface RDActionSheet ()
@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, strong) UIView *blackOutView;
- (void)setupButtons;
- (void)setupBackground;
- (UIView *)buildBlackOutViewWithFrame:(CGRect)frame;
- (UIButton *)buildButtonWithTitle:(NSString *)title;
- (UIButton *)buildCancelButtonWithTitle:(NSString *)title;
- (UIButton *)buildPrimaryButtonWithTitle:(NSString *)title;
- (UIButton *)buildDestroyButtonWithTitle:(NSString *)title;
- (CGFloat)calculateSheetHeight;
- (void)buttonWasPressed:(id)button;
@end
const CGFloat kButtonPadding = 10;
const CGFloat kButtonHeight = 47;
const CGFloat kPortrainButtonWidth = 300;
const CGFloat kLandscapeButtonWidth = 450;
const CGFloat kActionSheetAnimationTime = 0.2;
const CGFloat kBlackoutViewFadeInOpacity = 0.6;
@implementation RDActionSheet
@synthesize delegate;
@synthesize callbackBlock;
@synthesize buttons;
@synthesize blackOutView;
#pragma mark - Initialization
- (id)init {
self = [super init];
if (self) {
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.buttons = [NSMutableArray array];
self.opaque = YES;
}
return self;
}
- (id)initWithDelegate:(NSObject<RDActionSheetDelegate> *)aDelegate cancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destroyButtonTitle:(NSString *)destroyButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [self initWithCancelButtonTitle:cancelButtonTitle primaryButtonTitle:primaryButtonTitle destroyButtonTitle:destroyButtonTitle otherButtonTitles:otherButtonTitles];
if (self) {
self.delegate = aDelegate;
}
return self;
}
- (id)initWithCancelButtonTitle:(NSString *)cancelButtonTitle primaryButtonTitle:(NSString *)primaryButtonTitle destroyButtonTitle:(NSString *)destroyButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
self = [self init];
if (self) {
// Build normal buttons
va_list argumentList;
va_start(argumentList, otherButtonTitles);
NSString *argString = otherButtonTitles;
while (argString != nil) {
UIButton *button = [self buildButtonWithTitle:argString];
[self.buttons addObject:button];
argString = va_arg(argumentList, NSString *);
}
va_end(argumentList);
// Build cancel button
UIButton *cancelButton = [self buildCancelButtonWithTitle:cancelButtonTitle];
[self.buttons insertObject:cancelButton atIndex:0];
// Add primary button
if (primaryButtonTitle) {
UIButton *primaryButton = [self buildPrimaryButtonWithTitle:primaryButtonTitle];
[self.buttons addObject:primaryButton];
}
// Add destroy button
if (destroyButtonTitle) {
UIButton *destroyButton = [self buildDestroyButtonWithTitle:destroyButtonTitle];
[self.buttons insertObject:destroyButton atIndex:1];
}
}
return self;
}
#pragma mark - View setup
- (void)layoutSubviews {
[self setupBackground];
[self setupButtons];
}
- (void)setupBackground {
UIImage *backgroundImage = [[UIImage imageNamed:@"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.frame.size.width, self.frame.size.height), YES, 0);
CGContextRef con = UIGraphicsGetCurrentContext();
// Fill the context
UIColor *fillColor = [UIColor colorWithRed:18/255.0 green:18/255.0 blue:18/255.0 alpha:1];
CGContextSetFillColorWithColor(con, fillColor.CGColor);
CGContextFillRect(con, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
// Draw gradient
[backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, backgroundImage.size.height)];
// Draw Line
CGFloat lineYAxis = self.frame.size.height - (kButtonPadding * 2 + kButtonHeight);
CGContextBeginPath(con);
CGContextSetStrokeColorWithColor(con, [UIColor blackColor].CGColor);
CGContextSetLineWidth(con, 1);
CGContextMoveToPoint(con, 0, lineYAxis);
CGContextAddLineToPoint(con, self.frame.size.width, lineYAxis);
CGContextStrokePath(con);
CGContextBeginPath(con);
UIColor *strokeColor = [UIColor colorWithRed:42/255.0 green:45/255.0 blue:48/255.0 alpha:1];
CGContextSetStrokeColorWithColor(con, strokeColor.CGColor);
CGContextSetLineWidth(con, 1);
CGContextMoveToPoint(con, 0, lineYAxis + 1);
CGContextAddLineToPoint(con, self.frame.size.width, lineYAxis + 1);
CGContextStrokePath(con);
UIImage *finishedBackground = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *background = [[UIImageView alloc] initWithImage:finishedBackground];
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self insertSubview:background atIndex:0];
}
- (void)setupButtons {
CGFloat yOffset = self.frame.size.height - kButtonPadding - floorf(kButtonHeight/2);
BOOL cancelButton = YES;
for (UIButton *button in self.buttons) {
CGFloat buttonWidth;
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
buttonWidth = kLandscapeButtonWidth;
}
else {
buttonWidth = kPortrainButtonWidth;
}
button.frame = CGRectMake(0, 0, buttonWidth, kButtonHeight);
button.center = CGPointMake(self.center.x, yOffset);
[self addSubview:button];
yOffset -= button.frame.size.height + kButtonPadding;
// We draw a line above the cancel button so add an extra kButtonPadding
if (cancelButton == YES) {
yOffset -= kButtonPadding;
cancelButton = NO;
}
}
}
#pragma mark - Blackout view builder
- (UIView *)buildBlackOutViewWithFrame:(CGRect)frame {
UIView *view = [[UIView alloc] initWithFrame:frame];
view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
view.backgroundColor = [UIColor blackColor];
view.opaque = YES;
view.alpha = 0;
return view;
}
#pragma mark - Button builders
- (UIButton *)buildButtonWithTitle:(NSString *)title {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:title forState:UIControlStateNormal];
button.accessibilityLabel = title;
button.opaque = YES;
[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
UIColor *titleColor = [UIColor colorWithRed:18/255.0 green:22/255.0 blue:26/255.0 alpha:1];
[button setTitleColor:titleColor forState:UIControlStateNormal];
UIImage *backgroundImage = [[UIImage imageNamed:@"SheetButtonGeneric.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *touchBackgroundImage = [[UIImage imageNamed:@"SheetButtonGenericTouch.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted];
button.titleLabel.layer.shadowColor = [UIColor whiteColor].CGColor;
button.titleLabel.layer.shadowRadius = 0.0;
button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, 1.0);
button.titleLabel.layer.shadowOpacity = 0.5;
return button;
}
- (UIButton *)buildCancelButtonWithTitle:(NSString *)title {
UIButton *button = [self buildButtonWithTitle:title];
[button removeTarget:self action:@selector(buttonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(cancelActionSheet) forControlEvents:UIControlEventTouchUpInside];
UIImage *backgroundImage = [[UIImage imageNamed:@"SheetButtonDismiss.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *touchBackgroundImage = [[UIImage imageNamed:@"SheetButtonDismissTouch.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted];
button.titleLabel.layer.shadowOpacity = 0.3;
return button;
}
- (UIButton *)buildPrimaryButtonWithTitle:(NSString *)title {
UIButton *button = [self buildButtonWithTitle:title];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIImage *backgroundImage = [[UIImage imageNamed:@"SheetButtonPrimary.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *touchBackgroundImage = [[UIImage imageNamed:@"SheetButtonPrimaryTouch.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted];
button.titleLabel.layer.shadowColor = [UIColor blackColor].CGColor;
button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0);
return button;
}
- (UIButton *)buildDestroyButtonWithTitle:(NSString *)title {
UIButton *button = [self buildButtonWithTitle:title];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIImage *backgroundImage = [[UIImage imageNamed:@"SheetButtonDestroy.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
UIImage *touchBackgroundImage = [[UIImage imageNamed:@"SheetButtonDestroyTouch.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:1];
[button setBackgroundImage:touchBackgroundImage forState:UIControlStateHighlighted];
button.titleLabel.layer.shadowColor = [UIColor blackColor].CGColor;
button.titleLabel.layer.shadowOffset = CGSizeMake(0.0, -1.0);
return button;
}
#pragma mark - Button actions
- (void)buttonWasPressed:(id)button {
NSInteger buttonIndex = [self.buttons indexOfObject:button] - 1;
if (self.callbackBlock) {
self.callbackBlock(RDActionSheetButtonResultSelected, buttonIndex);
}
else {
if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
[self.delegate actionSheet:self clickedButtonAtIndex:buttonIndex];
}
}
[self cancelActionSheet];
}
- (void)cancelActionSheet {
[UIView animateWithDuration:kActionSheetAnimationTime animations:^{
CGFloat endPosition = self.frame.origin.y + self.frame.size.height;
self.frame = CGRectMake(self.frame.origin.x, endPosition, self.frame.size.width, self.frame.size.height);
self.blackOutView.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
if (self.callbackBlock) {
self.callbackBlock(RDActionSheetResultResultCancelled, -1);
}
else {
if (self.delegate && [self.delegate respondsToSelector:@selector(actionSheetDidBecomeCancelled:)]) {
[self.delegate actionSheetDidBecomeCancelled:self];
}
}
}
#pragma mark - Present action sheet
- (void)showFrom:(UIView *)view {
CGFloat startPosition = view.bounds.origin.y + view.bounds.size.height;
self.frame = CGRectMake(0, startPosition, view.bounds.size.width, [self calculateSheetHeight]);
[view addSubview:self];
self.blackOutView = [self buildBlackOutViewWithFrame:view.bounds];
[view insertSubview:self.blackOutView belowSubview:self];
[UIView animateWithDuration:kActionSheetAnimationTime animations:^{
CGFloat endPosition = startPosition - self.frame.size.height;
self.frame = CGRectMake(self.frame.origin.x, endPosition, self.frame.size.width, self.frame.size.height);
self.blackOutView.alpha = kBlackoutViewFadeInOpacity;
}];
}
#pragma mark - Helpers
- (CGFloat)calculateSheetHeight {
return ((kButtonHeight * self.buttons.count) + (self.buttons.count * kButtonPadding) + kButtonHeight/2) + 4;
}
@end