123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- //
- // 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<ZCAlertAction *> *)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<ZCAlertAction *> *)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
|