|
| 1 | +// |
| 2 | +// CCActionSheet.m |
| 3 | +// CCActionSheet |
| 4 | +// |
| 5 | +// Created by maxmoo on 16/1/29. |
| 6 | +// Copyright © 2016年 maxmoo. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "CCActionSheet.h" |
| 10 | + |
| 11 | +#define CC_SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) |
| 12 | +#define CC_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) |
| 13 | + |
| 14 | +#define cellHeight 45 |
| 15 | + |
| 16 | +@interface CCActionSheet() |
| 17 | + |
| 18 | +@property (nonatomic, strong) UIWindow *sheetWindow; |
| 19 | +@property (nonatomic, strong) NSArray *selectArray; |
| 20 | +@property (nonatomic, strong) NSString *cancelString; |
| 21 | +@property (nonatomic, strong) UIView *sheetView; |
| 22 | +@property (nonatomic, strong) UIView *backView; |
| 23 | +@property (nonatomic, strong) UITapGestureRecognizer *tapGesture; |
| 24 | + |
| 25 | +@end |
| 26 | + |
| 27 | +@implementation CCActionSheet |
| 28 | + |
| 29 | ++ (instancetype)shareSheet{ |
| 30 | + static id shareSheet; |
| 31 | + static dispatch_once_t once; |
| 32 | + dispatch_once(&once, ^{ |
| 33 | + shareSheet = [[[self class] alloc] init]; |
| 34 | + }); |
| 35 | + return shareSheet; |
| 36 | +} |
| 37 | + |
| 38 | +- (void)cc_actionSheetWithSelectArray:(NSArray *)array cancelTitle:(NSString *)cancel{ |
| 39 | + |
| 40 | + self.selectArray = [NSArray arrayWithArray:array]; |
| 41 | + self.cancelString = cancel; |
| 42 | + |
| 43 | + if (!_sheetWindow) { |
| 44 | + [self initSheetWindow]; |
| 45 | + } |
| 46 | + _sheetWindow.hidden = NO; |
| 47 | + |
| 48 | + [self showSheetWithAnimation]; |
| 49 | +} |
| 50 | + |
| 51 | +- (void)initSheetWindow{ |
| 52 | + _sheetWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, CC_SCREEN_WIDTH, CC_SCREEN_HEIGHT)]; |
| 53 | + _sheetWindow.windowLevel = UIWindowLevelStatusBar; |
| 54 | + _sheetWindow.backgroundColor = [UIColor clearColor]; |
| 55 | + |
| 56 | + _sheetWindow.hidden = YES; |
| 57 | + |
| 58 | + //zhezhao |
| 59 | + _backView = [[UIView alloc] initWithFrame:_sheetWindow.bounds]; |
| 60 | + _backView.backgroundColor = [UIColor blackColor]; |
| 61 | + _backView.alpha = 0.0; |
| 62 | + [_sheetWindow addSubview:_backView]; |
| 63 | + |
| 64 | + _tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SingleTap:)]; |
| 65 | + _tapGesture.numberOfTapsRequired = 1; |
| 66 | + [_backView addGestureRecognizer:_tapGesture]; |
| 67 | + |
| 68 | + UIView *selectView = [self creatSelectButton]; |
| 69 | + |
| 70 | + [_sheetWindow addSubview:selectView]; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +- (void)showSheetWithAnimation{ |
| 75 | + CGFloat viewHeight = cellHeight * (self.selectArray.count+1) + 5 + (self.selectArray.count - 2) * 2; |
| 76 | + [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ |
| 77 | + _sheetView.frame = CGRectMake(0, CC_SCREEN_HEIGHT - viewHeight, CC_SCREEN_WIDTH, viewHeight); |
| 78 | + _backView.alpha = 0.2; |
| 79 | + } completion:^(BOOL finished) { |
| 80 | + |
| 81 | + }]; |
| 82 | +} |
| 83 | + |
| 84 | +- (void)hidSheetWithAnimation{ |
| 85 | + CGFloat viewHeight = cellHeight * (self.selectArray.count+1) + 5 + (self.selectArray.count - 2) * 2; |
| 86 | + [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ |
| 87 | + _sheetView.frame = CGRectMake(0, CC_SCREEN_HEIGHT, CC_SCREEN_WIDTH, viewHeight); |
| 88 | + _backView.alpha = 0.0; |
| 89 | + } completion:^(BOOL finished) { |
| 90 | + [self hidActionSheet]; |
| 91 | + }]; |
| 92 | +} |
| 93 | + |
| 94 | +- (UIView *)creatSelectButton{ |
| 95 | + CGFloat viewHeight = cellHeight * (self.selectArray.count+1) + 5 + (self.selectArray.count - 2) * 2; |
| 96 | + _sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, CC_SCREEN_HEIGHT, CC_SCREEN_WIDTH, viewHeight)]; |
| 97 | + _sheetView.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]; |
| 98 | + |
| 99 | + for (int i = 0; i < self.selectArray.count; i++) { |
| 100 | + UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 101 | + button.frame = CGRectMake(0, i * (cellHeight+1), CC_SCREEN_WIDTH, cellHeight); |
| 102 | + [button setTitle:[NSString stringWithFormat:@"%@",self.selectArray[i]] forState:UIControlStateNormal]; |
| 103 | + [button setBackgroundColor:[UIColor whiteColor]]; |
| 104 | + [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; |
| 105 | + [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; |
| 106 | + [button addTarget:self action:@selector(buttonSelectAction:) forControlEvents:UIControlEventTouchUpInside]; |
| 107 | + button.tag = 1001+i; |
| 108 | + [_sheetView addSubview:button]; |
| 109 | + } |
| 110 | + |
| 111 | + UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]; |
| 112 | + cancelButton.frame = CGRectMake(0, viewHeight - cellHeight, CC_SCREEN_WIDTH, cellHeight); |
| 113 | + cancelButton.backgroundColor = [UIColor whiteColor]; |
| 114 | + [cancelButton setTitle:[NSString stringWithFormat:@"%@",self.cancelString] forState:UIControlStateNormal]; |
| 115 | + [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; |
| 116 | + [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; |
| 117 | + [cancelButton addTarget:self action:@selector(buttonSelectAction:) forControlEvents:UIControlEventTouchUpInside]; |
| 118 | + cancelButton.tag = 1000; |
| 119 | + [_sheetView addSubview:cancelButton]; |
| 120 | + |
| 121 | + return _sheetView; |
| 122 | +} |
| 123 | + |
| 124 | +- (void)buttonSelectAction:(UIButton *)btn{ |
| 125 | + UIButton *button = (UIButton *)btn; |
| 126 | + NSInteger index = button.tag - 1000; |
| 127 | + if (self.delegate && [self.delegate respondsToSelector:@selector(cc_actionSheetDidSelectedIndex:)]) { |
| 128 | + [self.delegate cc_actionSheetDidSelectedIndex:index]; |
| 129 | + } |
| 130 | + [self hidSheetWithAnimation]; |
| 131 | +} |
| 132 | + |
| 133 | +-(void)SingleTap:(UITapGestureRecognizer*)recognizer |
| 134 | +{ |
| 135 | + [self hidSheetWithAnimation]; |
| 136 | +} |
| 137 | + |
| 138 | +- (void)hidActionSheet{ |
| 139 | + _sheetWindow.hidden = YES; |
| 140 | + _sheetWindow = nil; |
| 141 | +} |
| 142 | + |
| 143 | +@end |
0 commit comments