123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // ZCAlertSheetBtnView.m
- // XLChat
- //
- // Created by 张灿 on 2017/11/6.
- // Copyright © 2017年 张灿. All rights reserved.
- //
- #import "ZCAlertSheetBtnView.h"
- #define ZCAlertSheetBtnHeight (40*KScreenWidth/320)//根据比例得到高度
- @implementation ZCAlertActionBtn
- + (ZCAlertActionBtn *)actionWithTitle:(NSString *)title type:(NSInteger)type andblock:(handleBtnBlock)block{
- ZCAlertActionBtn *action = [[ZCAlertActionBtn alloc]initWithTitle:title type:type andblock:block];
- return action;
- }
- - (id)initWithTitle:(NSString *)title type:(NSInteger)type andblock:(handleBtnBlock)block{
- if (self = [super init]) {
- self.block = block;
- self.title = title;
- self.type = type;
- }
- return self;
- }
- @end
- @interface ZCAlertSheetBtnView ()
- {
- UIView *_alertView;
- CGFloat _AllHeight;
- NSMutableArray *_marr;
- NSInteger _currentSelectIndex;
- NSMutableArray* _btnArray;
- }
- @end
- @implementation ZCAlertSheetBtnView
- - (UIWindow*)alertWindow{
- if (!_alertWindow) {
- _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
- _alertWindow.windowLevel = UIWindowLevelStatusBar+1;
- }
- return _alertWindow;
- }
- - (id)initWithTitle:(NSString *)title andShowCancelButton:(BOOL)show andAction:(NSArray<ZCAlertActionBtn *> *)arr selectIndex:(NSInteger)index column:(NSInteger)column{
- if (self = [super init]) {
- self.frame = [UIScreen mainScreen].bounds;
- _btnArray = [NSMutableArray array];
- _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(0xefeff4);
- [self addSubview:_alertView];
- _currentSelectIndex = index;
- CGFloat btnWidth = (KScreenWidth-(column+1)*15.0)/column;
- // NSInteger row = arr.count/column+1;//行数
- NSInteger row = (NSInteger)ceilf(arr.count/(CGFloat)column);
- if (title) {
- if (show) {
- _AllHeight = ZCAlertSheetBtnHeight+ZCAlertSheetBtnHeight+10+row*ZCAlertSheetBtnHeight+(row+1)*15;
- }else{
- _AllHeight = ZCAlertSheetBtnHeight+row*ZCAlertSheetBtnHeight+(row+1)*15;
- }
- _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
- UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCAlertSheetBtnHeight)];
- titleLabel.text = title;
- titleLabel.textAlignment = NSTextAlignmentCenter;
- titleLabel.font = [UIFont systemFontOfSize:15];
- titleLabel.textColor = HexColorFromRGB(0x333333);
- titleLabel.backgroundColor = [UIColor whiteColor];
- [_alertView addSubview:titleLabel];
- UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, ZCAlertSheetBtnHeight, KScreenWidth, row*ZCAlertSheetBtnHeight+(row+1)*15)];
- btnBkgView.backgroundColor = [UIColor whiteColor];
- [_alertView addSubview:btnBkgView];
- for (int i = 0; i < arr.count; i++) {
- ZCAlertActionBtn *action = arr[i];
- UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15+(i%column)*(btnWidth+15), 15+(i/column)*(15+ZCAlertSheetBtnHeight), btnWidth, ZCAlertSheetBtnHeight)];
- button.backgroundColor = LCGray;
- if(index==i){
- button.backgroundColor = ZYPinkColor;
- }
- button.layer.cornerRadius = 5.0;
- [button setTitle:action.title forState:UIControlStateNormal];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
- button.tag=i;
- [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
- [btnBkgView addSubview:button];
- [_btnArray addObject:button];
- }
- if (show) {
- UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
- cancel.frame = CGRectMake(0, ZCAlertSheetBtnHeight+10+row*ZCAlertSheetBtnHeight+(row+1)*15, KScreenWidth, ZCAlertSheetBtnHeight);
- [cancel setTitle:@"取消" forState:UIControlStateNormal];
- [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- cancel.backgroundColor = [UIColor whiteColor];
- [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
- [_alertView addSubview:cancel];
- }
- }else{
- if (show) {
- _AllHeight = ZCAlertSheetBtnHeight+10+row*ZCAlertSheetBtnHeight+(row+1)*15;
- }else{
- _AllHeight = row*ZCAlertSheetBtnHeight+(row+1)*15;
- }
- _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
- UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, row*ZCAlertSheetBtnHeight+(row+1)*15)];
- btnBkgView.backgroundColor = [UIColor whiteColor];
- [_alertView addSubview:btnBkgView];
- for (int i = 0; i < arr.count; i++) {
- ZCAlertActionBtn *action = arr[i];
- UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15+(i%column)*(btnWidth+15), 15+(i/column)*(15+ZCAlertSheetBtnHeight), btnWidth, ZCAlertSheetBtnHeight)];
- button.backgroundColor = HexColorFromRGB(0xb0b0b5);
- if(index==i){
- button.backgroundColor = HexColorFromRGB(0xfd86a4);
- }
- button.layer.cornerRadius = 5.0;
- [button setTitle:action.title forState:UIControlStateNormal];
- [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
- button.tag=i;
- [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
- [btnBkgView addSubview:button];
- [_btnArray addObject:button];
- }
- if (show) {
- UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
- cancel.frame = CGRectMake(0, 10+row*ZCAlertSheetBtnHeight+(row+1)*15, KScreenWidth, ZCAlertSheetBtnHeight);
- [cancel setTitle:@"取消" forState:UIControlStateNormal];
- [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- cancel.backgroundColor = [UIColor whiteColor];
- [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
- [_alertView addSubview:cancel];
- }
-
- }
-
-
- }
- return self;
- }
- - (void)youpaiftapClick:(UIButton *)button{
- UIButton* oldBtn = _btnArray[_currentSelectIndex];
- oldBtn.backgroundColor = LCGray;
- button.backgroundColor = ZYPinkColor;
- _currentSelectIndex = button.tag;
- ZCAlertActionBtn *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(action.type);
- }
- });
- }
- - (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, KScreenWidth, _AllHeight);
- } completion:^(BOOL finished) {
-
- }];
- }
- - (void)hide{
- [UIView animateWithDuration:0.3 animations:^{
- _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
- } completion:^(BOOL finished) {
- [self removeFromSuperview];
-
- _alertWindow.hidden = YES;
- _alertWindow = nil;
- }];
- }
- @end
|