// // JWAlertSheetView.m // JWlive // // Created by 蒋理智 on 16/3/25. // Copyright © 2016年 pican zhang. All rights reserved. // #import "ZCAlertSheetView.h" #define ZCAlertSheetHeight (49*KScreenWidth/320)//根据比例得到高度 @implementation ZCAlertAction + (ZCAlertAction *)actionWithTitle:(NSString *)title andblock:(handleBlock)block{ ZCAlertAction *action = [[ZCAlertAction alloc]initWithTitle:title andblock:block]; return action; } - (id)initWithTitle:(NSString *)title andblock:(handleBlock)block{ if (self = [super init]) { self.block = block; self.title = title; } return self; } @end @interface ZCAlertSheetView () { UIView *_alertView; CGFloat _AllHeight; NSMutableArray *_marr; } @end @implementation ZCAlertSheetView - (UIWindow*)alertWindow{ if (!_alertWindow) { //modify by leo --fix bug 20191008 //_alertWindow = [[UIApplication sharedApplication]keyWindow]; _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; _alertWindow.windowLevel = UIWindowLevelStatusBar+1; } return _alertWindow; } - (id)initWithTitle:(NSString *)title andShowCancelButton:(BOOL)show andAction:(NSArray *)arr{ if (self = [super init]) { self.frame = [UIScreen mainScreen].bounds; _marr = [NSMutableArray arrayWithArray:arr]; UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5]; // control.alpha = 0.5; [self addSubview:control]; _alertView = [[UIView alloc]init]; _alertView.backgroundColor = HexColorFromRGB(0xffffff); [self addSubview:_alertView]; if (title) { if (show) { _AllHeight = ZCAlertSheetHeight*(arr.count+1)+ZCAlertSheetHeight+10; }else{ _AllHeight = ZCAlertSheetHeight*(arr.count+1); } _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight); UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCAlertSheetHeight-1)]; titleLabel.text = title; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.font = [UIFont systemFontOfSize:16]; titleLabel.textColor = LZ273145Color; [_alertView addSubview:titleLabel]; for (int i = 0; i < arr.count; i++) { ZCAlertAction *action = arr[i]; UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight+ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)]; button.backgroundColor = LCWhiteColor; [button setTitle:action.title forState:UIControlStateNormal]; [button setTitleColor:LZ273145Color forState:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; button.tag=i; [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:button]; } if (show) { UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom]; cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count+1)+10, KScreenWidth, ZCAlertSheetHeight); [cancel setTitle:@"取消" forState:UIControlStateNormal]; [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal]; cancel.backgroundColor = LCWhiteColor; [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:cancel]; } }else{ if (show) { _AllHeight = ZCAlertSheetHeight*(arr.count)+ZCAlertSheetHeight+10; }else{ _AllHeight = ZCAlertSheetHeight*(arr.count); } _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight); for (int i = 0; i < arr.count; i++) { ZCAlertAction *action = arr[i]; UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)]; button.backgroundColor = LCWhiteColor; [button setTitle:action.title forState:UIControlStateNormal]; [button setTitleColor:LZ273145Color forState:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; button.tag=i; [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:button]; [button addLineWithColor:[HexColorFromRGB(0xffffff) colorWithAlphaComponent:0.29f] lineRect:CGRectMake(14.0f, ZCAlertSheetHeight-1 - 0.5f, KScreenWidth - 28.0f, 0.5f)]; } if (show) { UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom]; cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count)+10, KScreenWidth, ZCAlertSheetHeight); [cancel setTitle:@"取消" forState:UIControlStateNormal]; [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal]; cancel.backgroundColor = LCWhiteColor; [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:cancel]; } } CAShapeLayer *sectionmaskLayer = [CAShapeLayer layer]; UIBezierPath *sectionpath = [UIBezierPath bezierPathWithRoundedRect:_alertView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)]; sectionmaskLayer.path = sectionpath.CGPath; _alertView.layer.mask = sectionmaskLayer; } return self; } - (id)initWithAttrbuteTitle:(NSAttributedString *)attrbuteTitle andShowCancelButton:(BOOL)show andAction:(NSArray *)arr{ if (self = [super init]) { self.frame = [UIScreen mainScreen].bounds; _marr = [NSMutableArray arrayWithArray:arr]; UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5]; // control.alpha = 0.5; [self addSubview:control]; _alertView = [[UIView alloc]init]; _alertView.backgroundColor = LCWhiteColor; [self addSubview:_alertView]; if (attrbuteTitle) { if (show) { _AllHeight = ZCAlertSheetHeight*(arr.count+1)+ZCAlertSheetHeight+10; }else{ _AllHeight = ZCAlertSheetHeight*(arr.count+1); } _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight); UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCAlertSheetHeight-1)]; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.font = [UIFont systemFontOfSize:15]; titleLabel.textColor = HexColorFromRGB(0x9F9DA5); titleLabel.attributedText = attrbuteTitle; [_alertView addSubview:titleLabel]; for (int i = 0; i < arr.count; i++) { ZCAlertAction *action = arr[i]; UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight+ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)]; button.backgroundColor = LCWhiteColor; [button setTitle:action.title forState:UIControlStateNormal]; [button setTitleColor:LZ273145Color forState:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; button.tag=i; [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:button]; } if (show) { UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom]; cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count+1)+10, KScreenWidth, ZCAlertSheetHeight); [cancel setTitle:@"取消" forState:UIControlStateNormal]; [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal]; cancel.backgroundColor = LCWhiteColor; [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:cancel]; } }else{ if (show) { _AllHeight = ZCAlertSheetHeight*(arr.count)+ZCAlertSheetHeight+10; }else{ _AllHeight = ZCAlertSheetHeight*(arr.count); } _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight); for (int i = 0; i < arr.count; i++) { ZCAlertAction *action = arr[i]; UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)]; button.backgroundColor = LCWhiteColor; [button setTitle:action.title forState:UIControlStateNormal]; [button setTitleColor:LZ273145Color forState:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; button.tag=i; [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:button]; [button addLineWithColor:[HexColorFromRGB(0xffffff) colorWithAlphaComponent:0.29f] lineRect:CGRectMake(14.0f, ZCAlertSheetHeight-1 - 0.5f, KScreenWidth - 28.0f, 0.5f)]; } if (show) { UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom]; cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count)+10, KScreenWidth, ZCAlertSheetHeight); [cancel setTitle:@"取消" forState:UIControlStateNormal]; [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal]; cancel.backgroundColor = LCWhiteColor; [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside]; [_alertView addSubview:cancel]; } } CAShapeLayer *sectionmaskLayer = [CAShapeLayer layer]; UIBezierPath *sectionpath = [UIBezierPath bezierPathWithRoundedRect:_alertView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)]; sectionmaskLayer.path = sectionpath.CGPath; _alertView.layer.mask = sectionmaskLayer; } return self; } - (void)youpaiftapClick:(UIButton *)button{ ZCAlertAction *action = _marr[button.tag]; [self hide]; //加个延时操作防止先回调再hide,影响用户体验 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if (action.block) { action.block(); } }); } - (void)show{ [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; _alertWindow.hidden = NO; [_alertWindow addSubview:self]; [UIView animateWithDuration:0.3 animations:^{ _alertView.frame = CGRectMake(0, KScreenHeight-_AllHeight-SafeHeight, KScreenWidth, _AllHeight+SafeHeight); } completion:^(BOOL finished) { }]; } - (void)hide{ [UIView animateWithDuration:0.3 animations:^{ _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight+SafeHeight); } completion:^(BOOL finished) { [self removeFromSuperview]; _alertWindow.hidden = YES; _alertWindow = nil; }]; } @end