/
TBActionSheet.m
801 lines (714 loc) · 30.9 KB
/
TBActionSheet.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//
// TBActionSheet.m
//
// Created by 杨萧玉 on 15/11/17.
// Copyright © 2015年 杨萧玉. All rights reserved.
//
#import "TBActionSheet.h"
#import "UIImage+BoxBlur.h"
#import "TBMacro.h"
#import "TBActionContainer.h"
#import "TBActionBackground.h"
#import "TBActionSheetController.h"
#import "UIWindow+TBAdditions.h"
#import "UIView+TBAdditions.h"
const CGFloat bigFragment = 8;
const CGFloat smallFragment = 0.5;
const CGFloat headerVerticalSpace = 10;
const CGFloat blurRadius = 0.7;
@interface TBActionSheet ()
@property (nonatomic,readwrite,getter=isVisible) BOOL visible;
@property (nonatomic,nonnull,strong) TBActionContainer * actionContainer;
@property (nonatomic,nonnull,strong) TBActionBackground * background;
@property (nonatomic,nonnull,strong) NSMutableArray<TBActionButton *> *buttons;
@property (nonatomic,nonnull,strong) NSMutableArray<UIView *> *separators;
@property (nonatomic,strong,nullable,readwrite) UILabel *titleLabel;
@property (nonatomic,strong,nullable,readwrite) UILabel *messageLabel;
@property (weak, nonatomic, readwrite) UIWindow *previousKeyWindow;
@property (strong, nonatomic) UIWindow *window;
@end
@implementation TBActionSheet
+ (void)initialize
{
if (self != [TBActionSheet class]) {
return;
}
TBActionSheet *appearance = [self appearance];
appearance.buttonHeight = 56;
appearance.offsetY = - bigFragment;
appearance.tintColor = [UIColor blackColor];
appearance.destructiveButtonColor = [UIColor redColor];
appearance.cancelButtonColor = [UIColor blackColor];
appearance.sheetWidth = MIN(kScreenWidth, kScreenHeight) - 20;
appearance.backgroundTransparentEnabled = YES;
appearance.backgroundTouchClosureEnabled = YES;
appearance.blurEffectEnabled = YES;
appearance.rectCornerRadius = 10;
appearance.ambientColor = [UIColor colorWithWhite:1 alpha:0.65];
appearance.separatorColor = [UIColor clearColor];
appearance.animationDuration = 0.2;
appearance.animationDampingRatio = 1;
appearance.animationVelocity = 1;
appearance.supportedInterfaceOrientations = UIInterfaceOrientationMaskAll;
}
- (instancetype)init
{
self = [super initWithFrame:[UIScreen mainScreen].bounds];
if (self) {
self.backgroundColor = [UIColor clearColor];
_background = [[TBActionBackground alloc] initWithFrame:self.bounds];
[self addSubview:_background];
_actionContainer = [[TBActionContainer alloc] initWithSheet:self];
[self addSubview:_actionContainer];
_buttons = [NSMutableArray array];
_separators = [NSMutableArray array];
//set default values
_cancelButtonIndex = -1;
_destructiveButtonIndex = -1;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarDidChangeOrientation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
return self;
}
- (instancetype)initWithTitle:(NSString *)title delegate:(id<TBActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
va_list argList;
// 从 otherButtonTitles 开始遍历参数,不包括 otherButtonTitles 本身.
va_start(argList, otherButtonTitles);
self = [self initWithTitle:title message:nil delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle firstOtherButtonTitle:otherButtonTitles titleList:argList];
va_end(argList);
return self;
}
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id<TBActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
va_list argList;
// 从 otherButtonTitles 开始遍历参数,不包括 otherButtonTitles 本身.
va_start(argList, otherButtonTitles);
self = [self initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle firstOtherButtonTitle:otherButtonTitles titleList:argList];
va_end(argList);
return self;
}
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id<TBActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle firstOtherButtonTitle:(NSString *)firstOtherButtonTitle titleList:(va_list)argList
{
self = [self init];
if (self) {
_title = title;
_message = message;
_delegate = delegate;
if (destructiveButtonTitle) {
_destructiveButtonIndex = [self addButtonWithTitle:destructiveButtonTitle style:TBActionButtonStyleDestructive];
}
if (firstOtherButtonTitle) {// 第一个参数 firstOtherButtonTitle 是不属于参数列表的,
[self addButtonWithTitle:firstOtherButtonTitle style:TBActionButtonStyleDefault];
NSString* eachArg;
while ((eachArg = va_arg(argList, NSString*))) {// 从 args 中遍历出参数,NSString* 指明类型
[self addButtonWithTitle:eachArg style:TBActionButtonStyleDefault];
}
}
if (cancelButtonTitle) {
_cancelButtonIndex = [self addButtonWithTitle:cancelButtonTitle style:TBActionButtonStyleCancel];
}
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSInteger)addButtonWithTitle:(NSString *)title
{
return [self addButtonWithTitle:title style:TBActionButtonStyleDefault];
}
- (NSInteger)addButtonWithTitle:(NSString *)title style:(TBActionButtonStyle)style
{
return [self addButtonWithTitle:title style:style handler:nil];
}
- (NSInteger)addButtonWithTitle:(nullable NSString *)title style:(TBActionButtonStyle)style handler:(nullable void (^)( TBActionButton * button))handler
{
TBActionButton *button = [TBActionButton buttonWithTitle:title style:style handler:handler];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.buttons addObject:button];
NSInteger index = self.buttons.count - 1;
switch (style) {
case TBActionButtonStyleDefault: {
;
break;
}
case TBActionButtonStyleCancel: {
self.cancelButtonIndex = index;
break;
}
case TBActionButtonStyleDestructive: {
self.destructiveButtonIndex = index;
break;
}
default: {
break;
}
}
return index;
}
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex
{
if ([self isIndexValid:buttonIndex]) {
return self.buttons[buttonIndex].currentTitle;
}
return nil;
}
- (NSInteger)numberOfButtons
{
return self.buttons.count;
}
#pragma mark - getter&setter
- (void)setButtonFont:(UIFont *)buttonFont
{
if (buttonFont && [self buttonFont] != buttonFont) {
for (TBActionButton *btn in self.buttons) {
btn.titleLabel.font = buttonFont;
}
}
}
- (UIFont *)buttonFont
{
return self.buttons.lastObject.titleLabel.font;
}
- (NSInteger)firstOtherButtonIndex
{
for (int i=0; i<self.buttons.count; i++) {
if (self.buttons[i].style==TBActionButtonStyleDefault) {
return i;
}
}
return -1;
}
- (void)setSeparatorColor:(UIColor *)separatorColor
{
if (separatorColor && separatorColor != _separatorColor) {
_separatorColor = separatorColor;
for (UIView *separator in self.separators) {
separator.backgroundColor = separatorColor;
}
}
}
- (BOOL)isVisible
{
// action sheet is visible iff it's associated with a window
return !!self.window && self.window.rootViewController;
}
- (void)setBackgroundTouchClosureEnabled:(NSInteger)backgroundTouchClosureEnabled
{
_backgroundTouchClosureEnabled = backgroundTouchClosureEnabled;
self.background.userInteractionEnabled = backgroundTouchClosureEnabled;
}
#pragma mark - show action
/**
* 设定新的 UIWindow,并将 TBActionSheet 附加在上面
*/
- (void)setupNewWindow
{
if ([self isVisible]) {
return;
}
self.previousKeyWindow = [UIApplication sharedApplication].keyWindow;
[self.previousKeyWindow interruptGesture];
TBActionSheetController *actionSheetVC = [[TBActionSheetController alloc] initWithNibName:nil bundle:nil];
actionSheetVC.actionSheet = self;
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.window.opaque = NO;
self.window.rootViewController = actionSheetVC;
[self.window makeKeyAndVisible];
}
/**
* 设定所有内容的布局
*/
- (void)setupLayout
{
//将视图从上到下排列,计算当前排列的纵坐标
__block CGFloat lastY = 0;
//inline block, 减少代码冗余
void(^handleLabelFrameBlock)(UILabel *label, NSString *content) = ^(UILabel *label, NSString *content) {
//给一个比较大的高度,宽度不变
CGSize size =CGSizeMake(self.sheetWidth,1000);
//获取当前文本的属性
NSDictionary * tdic = @{NSFontAttributeName:label.font};
CGSize actualsize;
if (kiOS7Later) {
//iOS7方法,获取文本需要的size,限制宽度
actualsize =[content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
}
else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
actualsize = [content sizeWithFont:label.font];
#pragma GCC diagnostic pop
}
label.frame = CGRectMake(0, lastY, self.sheetWidth, actualsize.height);
lastY = CGRectGetMaxY(label.frame);
};
// 设定是否触碰背景关闭 ActionSheet
self.background.userInteractionEnabled = self.isBackgroundTouchClosureEnabled;
//移除所有 Separator
for (UIView *separator in self.separators) {
[separator removeFromSuperview];
}
[self.separators removeAllObjects];
//处理标题
if ([self hasTitle]) {
lastY += headerVerticalSpace;
if (!self.titleLabel) {
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, lastY, self.sheetWidth, 0)];
[self.actionContainer.header addSubview:self.titleLabel];
}
self.titleLabel.textColor = [UIColor colorWithWhite:0.56 alpha:1];
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.2) {
self.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightBold];
}
else {
self.titleLabel.font = [UIFont systemFontOfSize:13];
}
self.titleLabel.text = self.title;
self.titleLabel.numberOfLines = 0;
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
handleLabelFrameBlock(self.titleLabel, self.title);
}
//处理 message
if ([self hasMessage]) {
if (![self hasTitle]) {
lastY += headerVerticalSpace;
}
if (!self.messageLabel) {
self.messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, lastY, self.sheetWidth, 0)];
[self.actionContainer.header addSubview:self.messageLabel];
}
self.messageLabel.textColor = [UIColor colorWithWhite:0.56 alpha:1];
self.messageLabel.font = [UIFont systemFontOfSize:13];
self.messageLabel.text = self.message;
self.messageLabel.numberOfLines = 0;
self.messageLabel.backgroundColor = [UIColor clearColor];
self.messageLabel.textAlignment = NSTextAlignmentCenter;
handleLabelFrameBlock(self.messageLabel, self.message);
}
//处理title与自定义视图间的分割线
if ([self hasHeader]) {
lastY += headerVerticalSpace;
self.actionContainer.header.frame = CGRectMake(0, 0, self.sheetWidth, lastY);
if (self.buttons.firstObject.style == TBActionButtonStyleCancel && !self.customView) {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:YES];
lastY = CGRectGetMaxY(self.actionContainer.header.frame) + bigFragment;
}
else {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:NO];
lastY = CGRectGetMaxY(self.actionContainer.header.frame) + smallFragment;
}
}
//处理自定义视图
if (self.customView) {
self.actionContainer.custom.frame = CGRectMake(0, lastY, self.sheetWidth, self.customView.frame.size.height);
self.customView.center = CGPointMake(self.sheetWidth / 2, self.customView.frame.size.height/2);
[self.actionContainer.custom addSubview:self.customView];
if (self.buttons.firstObject.style == TBActionButtonStyleCancel) {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:YES];
lastY = CGRectGetMaxY(self.actionContainer.custom.frame) + bigFragment;
}
else {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:NO];
lastY = CGRectGetMaxY(self.actionContainer.custom.frame) + smallFragment;
}
}
//计算按钮坐标并添加样式
[self.buttons enumerateObjectsUsingBlock:^(TBActionButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//inline block, 减少代码冗余
void(^addDefaultSeparatorBlock)(void) = ^() {
//上一个 button 如果是 cancel
if (idx>0&&self.buttons[idx-1].style == TBActionButtonStyleCancel) {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:YES];
lastY += bigFragment;
}
else {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:NO];
lastY += smallFragment;
}
};
switch (obj.style) {
case TBActionButtonStyleDefault: {
[obj setTitleColor:self.tintColor forState:UIControlStateNormal];
if (idx != 0) {
addDefaultSeparatorBlock();
}
break;
}
case TBActionButtonStyleCancel: {
[obj setTitleColor:self.cancelButtonColor forState:UIControlStateNormal];
if (idx != 0) {
[self addSeparatorLineAt:CGPointMake(0, lastY) isBigFragment:YES];
lastY += bigFragment;
}
break;
}
case TBActionButtonStyleDestructive: {
[obj setTitleColor:self.destructiveButtonColor forState:UIControlStateNormal];
if (idx != 0) {
addDefaultSeparatorBlock();
}
break;
}
default: {
break;
}
}
obj.frame = CGRectMake(0, lastY, self.sheetWidth, self.buttonHeight);
lastY = CGRectGetMaxY(obj.frame);
[self.actionContainer addSubview:obj];
}];
if (self.offsetY < 0) {
lastY -= self.offsetY;
}
self.actionContainer.frame = CGRectMake(kContainerLeft, kScreenHeight, self.sheetWidth, lastY);
}
/**
* 设置毛玻璃效果、圆角、背景颜色等
*/
- (void)setupStyle
{
CGFloat containerHeight = self.actionContainer.bounds.size.height;
UIImage *originalBackgroundImage = [self screenShotRect:CGRectMake(kContainerLeft, kScreenHeight-containerHeight, self.sheetWidth, containerHeight)];
CGFloat heightLargerThanImage = containerHeight - originalBackgroundImage.size.height;// 计算 container 的高度超出截图的数值
// 清理容器内用于毛玻璃效果的视图
[self.actionContainer cleanTempViews];
if (!self.isBackgroundTransparentEnabled) {
if (self.isBlurEffectEnabled) {
if (![self.actionContainer useSystemBlurEffect]) {
UIImage *backgroundImage = [originalBackgroundImage drn_boxblurImageWithBlur:blurRadius withTintColor:[self.ambientColor colorWithAlphaComponent:0.5]];
self.actionContainer.image = backgroundImage;
}
}
else {
self.actionContainer.image = nil;
self.actionContainer.backgroundColor = [self.ambientColor colorWithAlphaComponent:0.5];
}
}
else {
self.actionContainer.image = nil;
self.actionContainer.backgroundColor = nil;
}
//清理容器内的圆角
self.actionContainer.header.tbRectCorner = TBRectCornerNone;
self.actionContainer.custom.tbRectCorner = TBRectCornerNone;
for (TBActionButton *button in self.buttons) {
button.tbRectCorner = TBRectCornerNone;
}
TBWeakSelf(self);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
TBStrongSelf(self);
//设置圆角
[self.buttons enumerateObjectsUsingBlock:^(TBActionButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (self.buttons.count == 1) {
obj.tbRectCorner |= TBRectCornerTop|TBRectCornerBottom;
if ([self hasHeader]) {
self.actionContainer.header.tbRectCorner |= self.customView ? TBRectCornerTop : TBRectCornerAll;
}
if (self.customView) {
self.actionContainer.custom.tbRectCorner |= [self hasHeader] ? TBRectCornerBottom : TBRectCornerAll;
}
}
else if (obj.style == TBActionButtonStyleCancel) {
obj.tbRectCorner = TBRectCornerTop|TBRectCornerBottom;
if (idx >= 1) {
self.buttons[idx - 1].tbRectCorner |= TBRectCornerBottom;
}
else {
if (self.customView) {
self.actionContainer.custom.tbRectCorner |= TBRectCornerBottom;
}
else if ([self hasHeader]) {
self.actionContainer.header.tbRectCorner |= TBRectCornerBottom;
}
}
if (idx + 1 <= self.buttons.count - 1) {
self.buttons[idx + 1].tbRectCorner |= TBRectCornerTop;
}
}
else if (idx == 0) {
if (![self hasHeader] && !self.customView) {
obj.tbRectCorner |= TBRectCornerTop;
}
}
else if (idx == self.buttons.count - 1) {
obj.tbRectCorner |= TBRectCornerBottom;
}
}];
for (TBActionButton *btn in self.buttons) {
[btn setCornerRadius:self.rectCornerRadius];
}
UIImage *(^cutOriginalBackgroundImageInRect)(CGRect frame) = ^UIImage *(CGRect sourceFrame) {
CGRect targetFrame;
if (heightLargerThanImage > 0) {
targetFrame = CGRectMake(sourceFrame.origin.x, sourceFrame.origin.y - heightLargerThanImage, sourceFrame.size.width, sourceFrame.size.height);
}
else {
targetFrame = sourceFrame;
}
UIImage *cuttedImage = [self cutFromImage:originalBackgroundImage inRect:targetFrame];
return cuttedImage;
};
//设置背景风格
if ([self hasHeader]) {
self.actionContainer.header.tbRectCorner |= TBRectCornerTop;
if (self.isBlurEffectEnabled && self.isBackgroundTransparentEnabled) {
self.actionContainer.header.backgroundColor = nil;
if (![self.actionContainer useSystemBlurEffectUnderView:self.actionContainer.header]) {
UIImage *backgroundImage = [cutOriginalBackgroundImageInRect(self.actionContainer.header.frame) drn_boxblurImageWithBlur:blurRadius withTintColor:self.ambientColor];
self.actionContainer.header.image = backgroundImage;
}
else {
self.actionContainer.header.image = nil;
}
}
else {
self.actionContainer.header.image = nil;
self.actionContainer.header.backgroundColor = self.ambientColor;
}
}
if (self.customView) {
if (![self hasHeader]) {
self.actionContainer.custom.tbRectCorner |= TBRectCornerTop;
}
if (self.isBlurEffectEnabled && self.isBackgroundTransparentEnabled) {
self.actionContainer.custom.backgroundColor = nil;
if (![self.actionContainer useSystemBlurEffectUnderView:self.actionContainer.custom]) {
UIImage *backgroundImage = [cutOriginalBackgroundImageInRect(self.actionContainer.custom.frame) drn_boxblurImageWithBlur:blurRadius withTintColor:self.ambientColor];
self.actionContainer.custom.image = backgroundImage;
}
else {
self.actionContainer.custom.image = nil;
}
}
else {
self.actionContainer.custom.image = nil;
self.actionContainer.custom.backgroundColor = self.ambientColor;
}
}
[self.actionContainer.header setCornerRadius:self.rectCornerRadius];
[self.actionContainer.custom setCornerRadius:self.rectCornerRadius];
[self.buttons enumerateObjectsUsingBlock:^(TBActionButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (self.isBlurEffectEnabled && self.isBackgroundTransparentEnabled) {
obj.normalColor = nil;
obj.highlightedColor = nil;
if (![self.actionContainer useSystemBlurEffectUnderView:obj]) {
UIImage *cuttedImage = cutOriginalBackgroundImageInRect(obj.frame);
UIImage *backgroundImageNormal = [cuttedImage drn_boxblurImageWithBlur:blurRadius withTintColor:self.ambientColor];
UIImage *backgroundImageHighlighted = [cuttedImage drn_boxblurImageWithBlur:blurRadius withTintColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
[obj setBackgroundImage:backgroundImageNormal forState:UIControlStateNormal];
[obj setBackgroundImage:backgroundImageHighlighted forState:UIControlStateHighlighted];
obj.backgroundColor = [UIColor clearColor];
}
else {
[obj setBackgroundImage:nil forState:UIControlStateNormal];
[obj setBackgroundImage:nil forState:UIControlStateHighlighted];
}
}
else {
[obj setBackgroundImage:nil forState:UIControlStateNormal];
[obj setBackgroundImage:nil forState:UIControlStateHighlighted];
obj.normalColor = self.ambientColor;
obj.highlightedColor = [UIColor colorWithWhite:0.5 alpha:0.5];
}
}];
});
}
- (void)setupContainerFrame
{
self.actionContainer.frame = CGRectMake(kContainerLeft, kScreenHeight - self.actionContainer.frame.size.height - (!kiOS7Later? 20: 0), self.actionContainer.frame.size.width, self.actionContainer.frame.size.height);
}
/**
* 显示 ActionSheet
*/
- (void)show
{
if ([self.delegate respondsToSelector:@selector(willPresentAlertView:)]) {
[self.delegate willPresentActionSheet:self];
}
[self setupNewWindow];
[self setupLayout];
[self setupStyle];
//弹出 ActionSheet 动画
void(^animations)(void) = ^() {
self.background.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[self setupContainerFrame];
};
void(^completion)(BOOL finished) = ^(BOOL finished) {
if ([self.delegate respondsToSelector:@selector(didPresentActionSheet:)]) {
[self.delegate didPresentActionSheet:self];
}
self.visible = YES;
};
if (kiOS7Later) {
[UIView animateWithDuration:self.animationDuration delay:0 usingSpringWithDamping:self.animationDampingRatio initialSpringVelocity:self.animationVelocity options:UIViewAnimationOptionCurveEaseInOut animations:animations completion:completion];
}
else {
[UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:animations completion:completion];
}
}
/**
* 显示 ActionSheet
*
* @param view 没有用到
*/
- (void)showInView:(UIView *)view
{
[self show];
}
#pragma mark - handle button press
/**
* 按钮点击事件,不要直接调用
*
* @param sender 点击的按钮对象
*/
- (void)buttonTapped:(TBActionButton *)sender
{
if (![self isVisible]) {
return;
}
NSUInteger index = [self.buttons indexOfObject:sender];
[UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.background.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
self.actionContainer.frame = CGRectMake(kContainerLeft, kScreenHeight, self.actionContainer.frame.size.width, self.actionContainer.frame.size.height);
} completion:^(BOOL finished) {
//这里之所以把各种 delegate 调用都放在动画完成后是有原因的:为了支持在回调方法中 show 另一个 actionsheet,系统的 UIActionSheet 的调用时机也是如此。
if ([self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self willDismissWithButtonIndex:index];
}
self.window.rootViewController = nil;
[self.previousKeyWindow makeKeyAndVisible];
if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
[self.delegate actionSheet:self clickedButtonAtIndex:index];
}
if (sender.handler) {
__weak __typeof(TBActionButton *)weakSender = sender;
sender.handler(weakSender);
}
if ([self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self didDismissWithButtonIndex:index];
}
self.visible = NO;
}];
}
#pragma mark - handle close
/**
* 取消 ActionSheet 的方法
*/
- (void)close
{
if (![self isVisible]) {
return;
}
[UIView animateWithDuration:self.animationDuration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.background.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
self.actionContainer.frame = CGRectMake(kContainerLeft, kScreenHeight, self.actionContainer.frame.size.width, self.actionContainer.frame.size.height);
} completion:^(BOOL finished) {
self.window.rootViewController = nil;
[self.previousKeyWindow makeKeyAndVisible];
if ([self.delegate respondsToSelector:@selector(actionSheetCancel:)]) {
[self.delegate actionSheetCancel:self];
}
else if ([self isIndexValid:self.cancelButtonIndex]) {
if ([self.delegate respondsToSelector:@selector(actionSheet:clickedButtonAtIndex:)]) {
[self.delegate actionSheet:self clickedButtonAtIndex:self.cancelButtonIndex];
}
TBActionButton *button = self.buttons[self.cancelButtonIndex];
if (button.handler) {
__weak __typeof(TBActionButton *)weakButton = button;
button.handler(weakButton);
}
if ([self.delegate respondsToSelector:@selector(actionSheet:willDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self willDismissWithButtonIndex:self.cancelButtonIndex];
}
if ([self.delegate respondsToSelector:@selector(actionSheet:didDismissWithButtonIndex:)]) {
[self.delegate actionSheet:self didDismissWithButtonIndex:self.cancelButtonIndex];
}
}
self.visible = NO;
}];
}
#pragma mark - handle Notification
- (void)statusBarDidChangeOrientation:(NSNotification *)notification {
self.bounds = [UIScreen mainScreen].bounds;
self.background.frame = self.bounds;
[self setupContainerFrame];
}
#pragma mark - help methods
- (BOOL)hasTitle
{
return self.title && self.title.length > 0;
}
- (BOOL)hasMessage
{
return self.message && self.message.length > 0;
}
- (BOOL)hasHeader
{
return [self hasTitle] || [self hasMessage];
}
- (void)addSeparatorLineAt:(CGPoint) point isBigFragment:(BOOL) isBigFragment
{
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, self.sheetWidth, isBigFragment?bigFragment:smallFragment)];
separatorLine.backgroundColor = self.separatorColor;
[self.actionContainer addSubview:separatorLine];
[self.separators addObject:separatorLine];
}
/**
* 从区域截屏
*
* @param aRect 区域
* @param view 截取的 view
*
* @return 截取的图片
*/
- (UIImage *)screenShotRect:(CGRect)aRect
{
// 获取最上层的 UIViewController
UIViewController *topController = [self.previousKeyWindow currentViewController];
UIView *view = topController.view;
UIGraphicsBeginImageContext(view.bounds.size);
if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
const CGFloat crashMagicNumber = 0.3;// size 小于0.3 在 iOS7 上会导致 crash
if (view.frame.size.width >= crashMagicNumber && view.frame.size.height >= crashMagicNumber ) { // resolve iOS7 size crash
[view drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
}
}
else {/* iOS 6 */
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
}
UIImage *screenshotimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [self cutFromImage:screenshotimage inRect:aRect];
// return [self cutFromImage:[self.previousKeyWindow tb_snapshot] inRect:aRect];
}
/**
* 从图片中切图
*
* @param image 要被切的图片
* @param rect 这里可以设置想要截图的区域
*
* @return 截图
*/
- (UIImage *)cutFromImage:(UIImage *)image inRect:(CGRect) rect
{
CGImageRef imageRef = image.CGImage;
CGRect transRect = CGRectMake(rect.origin.x*image.scale, rect.origin.y*image.scale, rect.size.width*image.scale, rect.size.height*image.scale);
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, transRect);
UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect scale:image.scale orientation:UIImageOrientationUp];
NSData *imageViewData = UIImagePNGRepresentation(sendImage);
CGImageRelease(imageRefRect);
return [UIImage imageWithData:imageViewData];
// return sendImage;
}
- (BOOL)isIndexValid:(NSInteger) index
{
if (index >=0 && index < self.buttons.count) {
return YES;
}
return NO;
}
@end