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
|
//
// BlockActionSheet.m
//
//
#import "BlockActionSheet.h"
#import "BlockBackground.h"
#import "BlockUI.h"
@implementation BlockActionSheet
@synthesize view = _view;
@synthesize vignetteBackground = _vignetteBackground;
static UIImage *background = nil;
static UIFont *titleFont = nil;
static UIFont *buttonFont = nil;
#pragma mark - init
+ (void)initialize
{
if (self == [BlockActionSheet class])
{
background = [UIImage imageNamed:kActionSheetBackground];
background = [[background stretchableImageWithLeftCapWidth:0 topCapHeight:kActionSheetBackgroundCapHeight] retain];
titleFont = [kActionSheetTitleFont retain];
buttonFont = [kActionSheetButtonFont retain];
}
}
+ (id)sheetWithTitle:(NSString *)title
{
return [[[BlockActionSheet alloc] initWithTitle:title] autorelease];
}
- (id)initWithTitle:(NSString *)title
{
if ((self = [super init]))
{
UIWindow *parentView = [BlockBackground sharedInstance];
CGRect frame = parentView.bounds;
_view = [[UIView alloc] initWithFrame:frame];
_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_blocks = [[NSMutableArray alloc] init];
_height = kActionSheetTopMargin;
if (title)
{
CGSize size = [title sizeWithFont:titleFont
constrainedToSize:CGSizeMake(frame.size.width-kActionSheetBorder*2, 1000)
lineBreakMode:NSLineBreakByWordWrapping];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kActionSheetBorder, _height, frame.size.width-kActionSheetBorder*2, size.height)];
labelView.font = titleFont;
labelView.numberOfLines = 0;
labelView.lineBreakMode = NSLineBreakByWordWrapping;
labelView.textColor = kActionSheetTitleTextColor;
labelView.backgroundColor = [UIColor clearColor];
labelView.textAlignment = NSTextAlignmentCenter;
labelView.shadowColor = kActionSheetTitleShadowColor;
labelView.shadowOffset = kActionSheetTitleShadowOffset;
labelView.text = title;
labelView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[_view addSubview:labelView];
[labelView release];
_height += size.height + 5;
}
_vignetteBackground = NO;
}
return self;
}
- (void) dealloc
{
[_view release];
[_blocks release];
[super dealloc];
}
- (NSUInteger)buttonCount
{
return _blocks.count;
}
- (void)addButtonWithTitle:(NSString *)title color:(NSString*)color block:(void (^)())block atIndex:(NSInteger)index
{
if (index >= 0)
{
[_blocks insertObject:[NSArray arrayWithObjects:
block ? [[block copy] autorelease] : [NSNull null],
title,
color,
nil]
atIndex:index];
}
else
{
[_blocks addObject:[NSArray arrayWithObjects:
block ? [[block copy] autorelease] : [NSNull null],
title,
color,
nil]];
}
}
- (void)setDestructiveButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:@"red" block:block atIndex:-1];
}
- (void)setCancelButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:@"black" block:block atIndex:-1];
}
- (void)addButtonWithTitle:(NSString *)title block:(void (^)())block
{
[self addButtonWithTitle:title color:@"gray" block:block atIndex:-1];
}
- (void)setDestructiveButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block
{
[self addButtonWithTitle:title color:@"red" block:block atIndex:index];
}
- (void)setCancelButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block
{
[self addButtonWithTitle:title color:@"black" block:block atIndex:index];
}
- (void)addButtonWithTitle:(NSString *)title atIndex:(NSInteger)index block:(void (^)())block
{
[self addButtonWithTitle:title color:@"gray" block:block atIndex:index];
}
- (void)showInView:(UIView *)view
{
NSUInteger i = 1;
for (NSArray *block in _blocks)
{
NSString *title = [block objectAtIndex:1];
NSString *color = [block objectAtIndex:2];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"action-%@-button.png", color]];
image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width)>>1 topCapHeight:0];
UIImage *highlightedImage = [UIImage imageNamed:[NSString stringWithFormat:@"action-%@-button-highlighted.png", color]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(kActionSheetBorder, _height, _view.bounds.size.width-kActionSheetBorder*2, kActionSheetButtonHeight);
button.titleLabel.font = buttonFont;
if (IOS_LESS_THAN_6) {
#pragma clan diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
button.titleLabel.minimumFontSize = 10;
#pragma clan diagnostic pop
}
else {
button.titleLabel.minimumScaleFactor = 0.1;
}
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.shadowOffset = kActionSheetButtonShadowOffset;
button.backgroundColor = [UIColor clearColor];
button.tag = i++;
[button setBackgroundImage:image forState:UIControlStateNormal];
if (highlightedImage)
{
[button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
}
[button setTitleColor:kActionSheetButtonTextColor forState:UIControlStateNormal];
[button setTitleShadowColor:kActionSheetButtonShadowColor forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
button.accessibilityLabel = title;
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
button.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[_view addSubview:button];
_height += kActionSheetButtonHeight + kActionSheetBorder;
}
UIImageView *modalBackground = [[UIImageView alloc] initWithFrame:_view.bounds];
modalBackground.image = background;
modalBackground.contentMode = UIViewContentModeScaleToFill;
modalBackground.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_view insertSubview:modalBackground atIndex:0];
[modalBackground release];
[BlockBackground sharedInstance].vignetteBackground = _vignetteBackground;
[[BlockBackground sharedInstance] addToMainWindow:_view];
CGRect frame = _view.frame;
frame.origin.y = [BlockBackground sharedInstance].bounds.size.height;
frame.size.height = _height + kActionSheetBounce;
_view.frame = frame;
__block CGPoint center = _view.center;
center.y -= _height + kActionSheetBounce;
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[BlockBackground sharedInstance].alpha = 1.0f;
_view.center = center;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
center.y += kActionSheetBounce;
_view.center = center;
} completion:nil];
}];
[self retain];
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
if (buttonIndex >= 0 && buttonIndex < [_blocks count])
{
id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0];
if (![obj isEqual:[NSNull null]])
{
((void (^)())obj)();
}
}
if (animated)
{
CGPoint center = _view.center;
center.y += _view.bounds.size.height;
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
_view.center = center;
[[BlockBackground sharedInstance] reduceAlphaIfEmpty];
} completion:^(BOOL finished) {
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}];
}
else
{
[[BlockBackground sharedInstance] removeView:_view];
[_view release]; _view = nil;
[self autorelease];
}
}
#pragma mark - Action
- (void)buttonClicked:(id)sender
{
/* Run the button's block */
int buttonIndex = [(UIButton *)sender tag] - 1;
[self dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
@end
|