// // POImageTitleAlertView.m // powerone // // Created by SuperYann on 2023/5/16. // Copyright © 2023 onecloud.ltd. All rights reserved. // #define CellHeight 56.0 #import "MOImageTitleAlertView.h" #import "MOImageTitleAlertCell.h" #import "MOTextAlertCell.h" @interface MOImageTitleAlertView () /** */ @property (nonatomic, strong) UIView *backgroundView; /** */ @property (nonatomic, strong) NSArray *dataArray; /** 需要完成动画的图层 */ @property (nonatomic, strong) UIView *actionSheetView; /** */ @property (nonatomic, strong) UITableView *tableView; /** */ @property (nonatomic, strong) UIButton *bottomBtn; /** */ @property (nonatomic, assign) CGFloat tableViewHeight; /** */ @property (nonatomic, assign) CGFloat actionViewHeight; @property (nonatomic, assign) MOAlertViewType viewType; @end @implementation MOImageTitleAlertView - (instancetype)initForOperateSuccessWithFrame:(CGRect)frame ViewType:(MOAlertViewType)viewType DataArray:(NSArray *)dataArray { self = [super initWithFrame:frame]; if (self) { self.tableViewHeight = 0.0; self.viewType = viewType; self.dataArray = dataArray; self.backgroundView = [[UIView alloc] initWithFrame:frame]; self.backgroundView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.4]; [self addSubview:self.backgroundView]; self.actionSheetView = [[UIView alloc] init]; self.actionSheetView.backgroundColor = [MOTools colorWithHexString:@"#F3F4FA"]; self.actionSheetView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner; self.actionSheetView.layer.cornerRadius = 16.0; self.actionSheetView.layer.masksToBounds = YES; [self addSubview:self.actionSheetView]; [self.actionSheetView addSubview:self.bottomBtn]; [self.actionSheetView addSubview:self.tableView]; [self setMasonry]; } return self; } - (void)setMasonry { self.tableViewHeight = self.dataArray.count * CellHeight; self.actionViewHeight = self.tableViewHeight + 8.0 + CellHeight + HOME_KEY_HEIGHT; ///<设置界面约束 [self.actionSheetView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.equalTo(self); make.height.equalTo(@(self.actionViewHeight)); }]; [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.actionSheetView).offset(0); make.right.equalTo(self.actionSheetView).offset(0); make.height.equalTo(@(CellHeight)); make.bottom.equalTo(self.actionSheetView).offset(- HOME_KEY_HEIGHT); }]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.bottomBtn.mas_top).offset(-8); make.left.equalTo(self.actionSheetView).offset(0); make.right.equalTo(self.actionSheetView).offset(0); make.height.equalTo(@(self.tableViewHeight)); }]; } - (UITableView *)tableView { if (!_tableView) { _tableView= [[UITableView alloc] init]; if (@available(iOS 11.0, *)) { _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } _tableView.backgroundColor = [UIColor whiteColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.scrollEnabled = NO; _tableView.dataSource = self; _tableView.delegate = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; } return _tableView; } - (UIButton *)bottomBtn { if (!_bottomBtn) { _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _bottomBtn.backgroundColor = [UIColor whiteColor]; [_bottomBtn setTitle:NSLocalString(@"mimo_Cancel") forState:UIControlStateNormal]; [_bottomBtn setTitleColor:[MOTools colorWithHexString:@"#5C5E66" alpha:0.5] forState:UIControlStateNormal]; _bottomBtn.titleLabel.font = [MOTextTools regularFont:16]; [_bottomBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside]; } return _bottomBtn; } #pragma mark - Set - (void)setCancelBtnColor:(UIColor *)cancelBtnColor{ _cancelBtnColor = cancelBtnColor; [self.bottomBtn setTitleColor:cancelBtnColor forState:UIControlStateNormal]; } - (void)setTitleColor:(UIColor *)titleColor{ _titleColor = titleColor; [self.tableView reloadData]; } - (void)setCancelText:(NSString *)cancelText{ _cancelText = cancelText; [self.bottomBtn setTitle:cancelText forState:UIControlStateNormal]; } #pragma mark - UITableViewDelegate,UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return CellHeight; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dict = self.dataArray[indexPath.row]; if(self.viewType == MOImageAndTextCellType){ //图文 MOImageTitleAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:MOImageTitleAlertCell_ID]; if (cell == nil) { cell = [[MOImageTitleAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOImageTitleAlertCell_ID]; } [cell.icon setImage:[UIImage imageNamed:dict[@"icon"]]]; [cell.titleLab setText:dict[@"title"]]; if(self.titleColor){ cell.titleLab.textColor = self.titleColor; } return cell; } else{ //纯文字 MOTextAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:MOTextAlertCell_ID]; if (cell == nil) { cell = [[MOTextAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOTextAlertCell_ID]; } [cell.titleLab setText:dict[@"title"]]; if(self.titleColor){ cell.titleLab.textColor = self.titleColor; } return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; self.cellClickBlock ? self.cellClickBlock(indexPath.row) : nil; [self dismiss]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; if (![touch.view isEqual:self.actionSheetView]) { [self dismiss]; } } - (void)show { ///<展示方法 UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window; self.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT); CGRect actionSheetViewRect = self.actionSheetView.frame; actionSheetViewRect.origin.y = SCREENHEIGHT; self.actionSheetView.frame = actionSheetViewRect; WEAKSELF [UIView animateWithDuration:0.3 animations:^ { CGRect actionSheetViewRect = weakSelf.actionSheetView.frame; actionSheetViewRect.origin.y = SCREENHEIGHT - self.actionViewHeight; weakSelf.actionSheetView.frame = actionSheetViewRect; }]; [keyWindow addSubview:self]; } - (void)dismiss { ///<界面隐藏方法 WEAKSELF [UIView animateWithDuration:0.3 animations:^ { CGRect actionSheetViewRect = weakSelf.actionSheetView.frame; actionSheetViewRect.origin.y = SCREENHEIGHT; weakSelf.actionSheetView.frame = actionSheetViewRect; weakSelf.backgroundView.alpha = 0.0; } completion:^(BOOL finished) { [weakSelf removeFromSuperview]; }]; } @end