123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- //
- // Created by 刘超 on 15/4/26.
- // Copyright (c) 2015年 Leo. All rights reserved.
- //
- // Email: leoios@sina.com
- // GitHub: http://github.com/LeoiOS
- // 如有问题或建议请给我发Email, 或在该项目的GitHub主页lssues我, 谢谢:)
- //
- #import "LCActionSheet.h"
- // 按钮高度
- #define BUTTON_H 49.0f
- // 屏幕尺寸
- #define SCREEN_SIZE [UIScreen mainScreen].bounds.size
- // 颜色
- #define LCColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]
- @interface LCActionSheet () {
-
- /** 所有按钮 */
- NSArray *_buttonTitles;
-
- /** 暗黑色的view */
- UIView *_darkView;
-
- /** 所有按钮的底部view */
- UIView *_bottomView;
-
- /** 代理 */
- id<LCActionSheetDelegate> _delegate;
- }
- @property (nonatomic, strong) UIWindow *backWindow;
- @end
- @implementation LCActionSheet
- + (instancetype)sheetWithTitle:(NSString *)title buttonTitles:(NSArray *)titles redButtonIndex:(NSInteger)buttonIndex delegate:(id<LCActionSheetDelegate>)delegate {
-
- return [[self alloc] initWithTitle:title buttonTitles:titles redButtonIndex:buttonIndex delegate:delegate];
- }
- - (instancetype)initWithTitle:(NSString *)title
- buttonTitles:(NSArray *)titles
- redButtonIndex:(NSInteger)buttonIndex
- delegate:(id<LCActionSheetDelegate>)delegate {
-
- if (self = [super init]) {
-
- _delegate = delegate;
-
- // 暗黑色的view
- UIView *darkView = [[UIView alloc] init];
- [darkView setAlpha:0];
- [darkView setUserInteractionEnabled:NO];
- [darkView setFrame:(CGRect){0, 0, SCREEN_SIZE}];
- [darkView setBackgroundColor:LCColor(46, 49, 50)];
- [self addSubview:darkView];
- _darkView = darkView;
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss:)];
- [darkView addGestureRecognizer:tap];
-
- // 所有按钮的底部view
- UIView *bottomView = [[UIView alloc] init];
- [bottomView setBackgroundColor:LCColor(233, 233, 238)];
- [self addSubview:bottomView];
- _bottomView = bottomView;
-
- if (title) {
- _title = title;
- // 标题
- UILabel *label = [[UILabel alloc] init];
- [label setText:title];
- [label setTextColor:LCColor(111, 111, 111)];
- [label setTextAlignment:NSTextAlignmentCenter];
- [label setFont:[UIFont systemFontOfSize:13.0f]];
- [label setBackgroundColor:[UIColor whiteColor]];
- [label setFrame:CGRectMake(0, 0, SCREEN_SIZE.width, BUTTON_H)];
- [label setNumberOfLines:0];
- // label.lineBreakMode = UILineBreakModeWordWrap;
- [bottomView addSubview:label];
- }
-
- if (titles.count) {
-
- _buttonTitles = titles;
-
- for (int i = 0; i < titles.count; i++) {
-
- // 所有按钮
- UIButton *btn = [[UIButton alloc] init];
- [btn setTag:i];
- [btn setBackgroundColor:[UIColor whiteColor]];
- [btn setTitle:titles[i] forState:UIControlStateNormal];
- [[btn titleLabel] setFont:[UIFont systemFontOfSize:16.0f]];
-
- UIColor *titleColor = nil;
- if (i == buttonIndex) {
-
- titleColor = LCColor(255, 10, 10);
-
- } else {
-
- titleColor = [UIColor blackColor] ;
- }
- [btn setTitleColor:titleColor forState:UIControlStateNormal];
-
- [btn setBackgroundImage:[UIImage imageNamed:@"bgImage_HL"] forState:UIControlStateHighlighted];
- [btn addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
-
- CGFloat btnY = BUTTON_H * (i + (title ? 1 : 0));
- [btn setFrame:CGRectMake(0, btnY, SCREEN_SIZE.width, BUTTON_H)];
- [bottomView addSubview:btn];
- }
-
- for (int i = 0; i < titles.count; i++) {
-
- // 所有线条
- UIImageView *line = [[UIImageView alloc] init];
- [line setImage:[UIImage imageNamed:@"cellLine"]];
- [line setContentMode:UIViewContentModeCenter];
- CGFloat lineY = (i + (title ? 1 : 0)) * BUTTON_H;
- [line setFrame:CGRectMake(0, lineY, SCREEN_SIZE.width, 1.0f)];
- [bottomView addSubview:line];
- }
- }
-
- // 取消按钮
- UIButton *cancelBtn = [[UIButton alloc] init];
- [cancelBtn setTag:titles.count];
- [cancelBtn setBackgroundColor:[UIColor whiteColor]];
- [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
- [[cancelBtn titleLabel] setFont:[UIFont systemFontOfSize:16.0f]];
- [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [cancelBtn setBackgroundImage:[UIImage imageNamed:@"bgImage_HL"] forState:UIControlStateHighlighted];
- [cancelBtn addTarget:self action:@selector(didClickCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
-
- CGFloat btnY = BUTTON_H * (titles.count + (title ? 1 : 0)) + 5.0f;
- [cancelBtn setFrame:CGRectMake(0, btnY, SCREEN_SIZE.width, BUTTON_H+20)];
- [bottomView addSubview:cancelBtn];
-
- CGFloat bottomH = (title ? BUTTON_H : 0) + BUTTON_H * titles.count + BUTTON_H + 5.0f;
- [bottomView setFrame:CGRectMake(0, SCREEN_SIZE.height, SCREEN_SIZE.width, bottomH+20)];
-
- [self setFrame:(CGRect){0, 0, SCREEN_SIZE}];
- [self.backWindow addSubview:self];
- }
-
- return self;
- }
- - (UIWindow *)backWindow {
-
- if (_backWindow == nil) {
-
- _backWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
- _backWindow.windowLevel = UIWindowLevelStatusBar;
- _backWindow.backgroundColor = [UIColor clearColor];
- _backWindow.hidden = NO;
- }
-
- return _backWindow;
- }
- - (void)didClickBtn:(UIButton *)btn {
-
- [self dismiss:nil];
-
- if ([_delegate respondsToSelector:@selector(actionSheet:didClickedButton:)]) {
-
- [_delegate actionSheet:self didClickedButton:btn];
- }
- }
- - (void)dismiss:(UITapGestureRecognizer *)tap {
- if ([_delegate respondsToSelector:@selector(actionSheet:didClickedBlankPlace:)]) {
-
- [_delegate actionSheet:self didClickedBlankPlace:0];
- }
-
- RDS_WEAKSELF(weakSelf)
- [UIView animateWithDuration:0.3f
- delay:0
- options:UIViewAnimationOptionCurveEaseOut
- animations:^{
- __strong typeof(weakSelf)theSelf = weakSelf;
- [theSelf->_darkView setAlpha:0];
- [theSelf->_darkView setUserInteractionEnabled:NO];
-
- CGRect frame = theSelf->_bottomView.frame;
- frame.origin.y += frame.size.height;
- [theSelf->_bottomView setFrame:frame];
-
- }
- completion:^(BOOL finished) {
-
- __strong typeof(weakSelf)theSelf = weakSelf;
- theSelf->_backWindow.hidden = YES;
-
- [theSelf removeFromSuperview];
- }];
- }
- - (void)didClickCancelBtn:(UIButton *)btn {
-
- RDS_WEAKSELF(weakSelf)
- [UIView animateWithDuration:0.3f
- delay:0
- options:UIViewAnimationOptionCurveEaseOut
- animations:^{
- __strong typeof(weakSelf)theSelf = weakSelf;
- [theSelf->_darkView setAlpha:0];
- [theSelf->_darkView setUserInteractionEnabled:NO];
-
- CGRect frame = theSelf->_bottomView.frame;
- frame.origin.y += frame.size.height;
- [theSelf->_bottomView setFrame:frame];
-
- }
- completion:^(BOOL finished) {
-
- __strong typeof(weakSelf)theSelf = weakSelf;
- theSelf->_backWindow.hidden = YES;
-
- [theSelf removeFromSuperview];
-
- if ([theSelf->_delegate respondsToSelector:@selector(actionSheet:didClickedButton:)]) {
-
- [theSelf->_delegate actionSheet:theSelf didClickedButton:btn];
- }
- }];
- }
- - (void)show {
-
- _backWindow.hidden = NO;
- RDS_WEAKSELF(weakSelf)
- [UIView animateWithDuration:0.3f
- delay:0
- options:UIViewAnimationOptionCurveEaseOut
- animations:^{
- __strong typeof(weakSelf)theSelf = weakSelf;
- [theSelf->_darkView setAlpha:0.4f];
- [theSelf->_darkView setUserInteractionEnabled:YES];
-
- CGRect frame = theSelf->_bottomView.frame;
- frame.origin.y -= frame.size.height;
- [theSelf->_bottomView setFrame:frame];
-
- }
- completion:nil];
- }
- - (void)dealloc {
- DDLog(@"dealloc");
- }
- @end
|