MOImageTitleAlertView.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // POImageTitleAlertView.m
  3. // powerone
  4. //
  5. // Created by SuperYann on 2023/5/16.
  6. // Copyright © 2023 onecloud.ltd. All rights reserved.
  7. //
  8. #define CellHeight 56.0
  9. #import "MOImageTitleAlertView.h"
  10. #import "MOImageTitleAlertCell.h"
  11. #import "MOTextAlertCell.h"
  12. @interface MOImageTitleAlertView ()<UITableViewDelegate,UITableViewDataSource>
  13. /** */
  14. @property (nonatomic, strong) UIView *backgroundView;
  15. /** */
  16. @property (nonatomic, strong) NSArray *dataArray;
  17. /** 需要完成动画的图层 */
  18. @property (nonatomic, strong) UIView *actionSheetView;
  19. /** */
  20. @property (nonatomic, strong) UITableView *tableView;
  21. /** */
  22. @property (nonatomic, strong) UIButton *bottomBtn;
  23. /** */
  24. @property (nonatomic, assign) CGFloat tableViewHeight;
  25. /** */
  26. @property (nonatomic, assign) CGFloat actionViewHeight;
  27. @property (nonatomic, assign) MOAlertViewType viewType;
  28. @end
  29. @implementation MOImageTitleAlertView
  30. - (instancetype)initForOperateSuccessWithFrame:(CGRect)frame ViewType:(MOAlertViewType)viewType DataArray:(NSArray *)dataArray
  31. {
  32. self = [super initWithFrame:frame];
  33. if (self)
  34. {
  35. self.tableViewHeight = 0.0;
  36. self.viewType = viewType;
  37. self.dataArray = dataArray;
  38. self.backgroundView = [[UIView alloc] initWithFrame:frame];
  39. self.backgroundView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.4];
  40. [self addSubview:self.backgroundView];
  41. self.actionSheetView = [[UIView alloc] init];
  42. self.actionSheetView.backgroundColor = [MOTools colorWithHexString:@"#F3F4FA"];
  43. self.actionSheetView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
  44. self.actionSheetView.layer.cornerRadius = 16.0;
  45. self.actionSheetView.layer.masksToBounds = YES;
  46. [self addSubview:self.actionSheetView];
  47. [self.actionSheetView addSubview:self.bottomBtn];
  48. [self.actionSheetView addSubview:self.tableView];
  49. [self setMasonry];
  50. }
  51. return self;
  52. }
  53. - (void)setMasonry
  54. {
  55. self.tableViewHeight = self.dataArray.count * CellHeight;
  56. self.actionViewHeight = self.tableViewHeight + 8.0 + CellHeight + HOME_KEY_HEIGHT;
  57. ///<设置界面约束
  58. [self.actionSheetView mas_makeConstraints:^(MASConstraintMaker *make)
  59. {
  60. make.left.bottom.right.equalTo(self);
  61. make.height.equalTo(@(self.actionViewHeight));
  62. }];
  63. [self.bottomBtn mas_makeConstraints:^(MASConstraintMaker *make)
  64. {
  65. make.left.equalTo(self.actionSheetView).offset(0);
  66. make.right.equalTo(self.actionSheetView).offset(0);
  67. make.height.equalTo(@(CellHeight));
  68. make.bottom.equalTo(self.actionSheetView).offset(- HOME_KEY_HEIGHT);
  69. }];
  70. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make)
  71. {
  72. make.bottom.equalTo(self.bottomBtn.mas_top).offset(-8);
  73. make.left.equalTo(self.actionSheetView).offset(0);
  74. make.right.equalTo(self.actionSheetView).offset(0);
  75. make.height.equalTo(@(self.tableViewHeight));
  76. }];
  77. }
  78. - (UITableView *)tableView
  79. {
  80. if (!_tableView)
  81. {
  82. _tableView= [[UITableView alloc] init];
  83. if (@available(iOS 11.0, *))
  84. {
  85. _tableView.estimatedRowHeight = 0;
  86. _tableView.estimatedSectionFooterHeight = 0;
  87. _tableView.estimatedSectionHeaderHeight = 0;
  88. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  89. }
  90. _tableView.backgroundColor = [UIColor whiteColor];
  91. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  92. _tableView.scrollEnabled = NO;
  93. _tableView.dataSource = self;
  94. _tableView.delegate = self;
  95. _tableView.showsVerticalScrollIndicator = NO;
  96. _tableView.showsHorizontalScrollIndicator = NO;
  97. }
  98. return _tableView;
  99. }
  100. - (UIButton *)bottomBtn
  101. {
  102. if (!_bottomBtn)
  103. {
  104. _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  105. _bottomBtn.backgroundColor = [UIColor whiteColor];
  106. [_bottomBtn setTitle:NSLocalString(@"mimo_Cancel") forState:UIControlStateNormal];
  107. [_bottomBtn setTitleColor:[MOTools colorWithHexString:@"#5C5E66" alpha:0.5] forState:UIControlStateNormal];
  108. _bottomBtn.titleLabel.font = [MOTextTools regularFont:16];
  109. [_bottomBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
  110. }
  111. return _bottomBtn;
  112. }
  113. #pragma mark - Set
  114. - (void)setCancelBtnColor:(UIColor *)cancelBtnColor{
  115. _cancelBtnColor = cancelBtnColor;
  116. [self.bottomBtn setTitleColor:cancelBtnColor forState:UIControlStateNormal];
  117. }
  118. - (void)setTitleColor:(UIColor *)titleColor{
  119. _titleColor = titleColor;
  120. [self.tableView reloadData];
  121. }
  122. - (void)setCancelText:(NSString *)cancelText{
  123. _cancelText = cancelText;
  124. [self.bottomBtn setTitle:cancelText forState:UIControlStateNormal];
  125. }
  126. #pragma mark - UITableViewDelegate,UITableViewDataSource
  127. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  128. {
  129. return 1;
  130. }
  131. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  132. {
  133. return self.dataArray.count;
  134. }
  135. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. return CellHeight;
  138. }
  139. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. NSDictionary *dict = self.dataArray[indexPath.row];
  142. if(self.viewType == MOImageAndTextCellType){
  143. //图文
  144. MOImageTitleAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:MOImageTitleAlertCell_ID];
  145. if (cell == nil)
  146. {
  147. cell = [[MOImageTitleAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOImageTitleAlertCell_ID];
  148. }
  149. [cell.icon setImage:[UIImage imageNamed:dict[@"icon"]]];
  150. [cell.titleLab setText:dict[@"title"]];
  151. if(self.titleColor){
  152. cell.titleLab.textColor = self.titleColor;
  153. }
  154. return cell;
  155. }
  156. else{
  157. //纯文字
  158. MOTextAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:MOTextAlertCell_ID];
  159. if (cell == nil)
  160. {
  161. cell = [[MOTextAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOTextAlertCell_ID];
  162. }
  163. [cell.titleLab setText:dict[@"title"]];
  164. if(self.titleColor){
  165. cell.titleLab.textColor = self.titleColor;
  166. }
  167. return cell;
  168. }
  169. }
  170. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  173. self.cellClickBlock ? self.cellClickBlock(indexPath.row) : nil;
  174. [self dismiss];
  175. }
  176. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  177. {
  178. UITouch *touch = [touches anyObject];
  179. if (![touch.view isEqual:self.actionSheetView])
  180. {
  181. [self dismiss];
  182. }
  183. }
  184. - (void)show
  185. {
  186. ///<展示方法
  187. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  188. self.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
  189. CGRect actionSheetViewRect = self.actionSheetView.frame;
  190. actionSheetViewRect.origin.y = SCREENHEIGHT;
  191. self.actionSheetView.frame = actionSheetViewRect;
  192. WEAKSELF
  193. [UIView animateWithDuration:0.3 animations:^
  194. {
  195. CGRect actionSheetViewRect = weakSelf.actionSheetView.frame;
  196. actionSheetViewRect.origin.y = SCREENHEIGHT - self.actionViewHeight;
  197. weakSelf.actionSheetView.frame = actionSheetViewRect;
  198. }];
  199. [keyWindow addSubview:self];
  200. }
  201. - (void)dismiss
  202. {
  203. ///<界面隐藏方法
  204. WEAKSELF
  205. [UIView animateWithDuration:0.3 animations:^
  206. {
  207. CGRect actionSheetViewRect = weakSelf.actionSheetView.frame;
  208. actionSheetViewRect.origin.y = SCREENHEIGHT;
  209. weakSelf.actionSheetView.frame = actionSheetViewRect;
  210. weakSelf.backgroundView.alpha = 0.0;
  211. } completion:^(BOOL finished)
  212. {
  213. [weakSelf removeFromSuperview];
  214. }];
  215. }
  216. @end