ZCAlertSheetBtnView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ZCAlertSheetBtnView.m
  3. // XLChat
  4. //
  5. // Created by 张灿 on 2017/11/6.
  6. // Copyright © 2017年 张灿. All rights reserved.
  7. //
  8. #import "ZCAlertSheetBtnView.h"
  9. #define ZCAlertSheetBtnHeight (40*KScreenWidth/320)//根据比例得到高度
  10. @implementation ZCAlertActionBtn
  11. + (ZCAlertActionBtn *)actionWithTitle:(NSString *)title type:(NSInteger)type andblock:(handleBtnBlock)block{
  12. ZCAlertActionBtn *action = [[ZCAlertActionBtn alloc]initWithTitle:title type:type andblock:block];
  13. return action;
  14. }
  15. - (id)initWithTitle:(NSString *)title type:(NSInteger)type andblock:(handleBtnBlock)block{
  16. if (self = [super init]) {
  17. self.block = block;
  18. self.title = title;
  19. self.type = type;
  20. }
  21. return self;
  22. }
  23. @end
  24. @interface ZCAlertSheetBtnView ()
  25. {
  26. UIView *_alertView;
  27. CGFloat _AllHeight;
  28. NSMutableArray *_marr;
  29. NSInteger _currentSelectIndex;
  30. NSMutableArray* _btnArray;
  31. }
  32. @end
  33. @implementation ZCAlertSheetBtnView
  34. - (UIWindow*)alertWindow{
  35. if (!_alertWindow) {
  36. _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  37. _alertWindow.windowLevel = UIWindowLevelStatusBar+1;
  38. }
  39. return _alertWindow;
  40. }
  41. - (id)initWithTitle:(NSString *)title andShowCancelButton:(BOOL)show andAction:(NSArray<ZCAlertActionBtn *> *)arr selectIndex:(NSInteger)index column:(NSInteger)column{
  42. if (self = [super init]) {
  43. self.frame = [UIScreen mainScreen].bounds;
  44. _btnArray = [NSMutableArray array];
  45. _marr = [NSMutableArray arrayWithArray:arr];
  46. UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  47. [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  48. control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5];
  49. // control.alpha = 0.5;
  50. [self addSubview:control];
  51. _alertView = [[UIView alloc]init];
  52. _alertView.backgroundColor = HexColorFromRGB(0xefeff4);
  53. [self addSubview:_alertView];
  54. _currentSelectIndex = index;
  55. CGFloat btnWidth = (KScreenWidth-(column+1)*15.0)/column;
  56. // NSInteger row = arr.count/column+1;//行数
  57. NSInteger row = (NSInteger)ceilf(arr.count/(CGFloat)column);
  58. if (title) {
  59. if (show) {
  60. _AllHeight = ZCAlertSheetBtnHeight+ZCAlertSheetBtnHeight+10+row*ZCAlertSheetBtnHeight+(row+1)*15;
  61. }else{
  62. _AllHeight = ZCAlertSheetBtnHeight+row*ZCAlertSheetBtnHeight+(row+1)*15;
  63. }
  64. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
  65. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCAlertSheetBtnHeight)];
  66. titleLabel.text = title;
  67. titleLabel.textAlignment = NSTextAlignmentCenter;
  68. titleLabel.font = [UIFont systemFontOfSize:15];
  69. titleLabel.textColor = HexColorFromRGB(0x333333);
  70. titleLabel.backgroundColor = [UIColor whiteColor];
  71. [_alertView addSubview:titleLabel];
  72. UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, ZCAlertSheetBtnHeight, KScreenWidth, row*ZCAlertSheetBtnHeight+(row+1)*15)];
  73. btnBkgView.backgroundColor = [UIColor whiteColor];
  74. [_alertView addSubview:btnBkgView];
  75. for (int i = 0; i < arr.count; i++) {
  76. ZCAlertActionBtn *action = arr[i];
  77. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15+(i%column)*(btnWidth+15), 15+(i/column)*(15+ZCAlertSheetBtnHeight), btnWidth, ZCAlertSheetBtnHeight)];
  78. button.backgroundColor = LCGray;
  79. if(index==i){
  80. button.backgroundColor = ZYPinkColor;
  81. }
  82. button.layer.cornerRadius = 5.0;
  83. [button setTitle:action.title forState:UIControlStateNormal];
  84. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  85. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  86. button.tag=i;
  87. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  88. [btnBkgView addSubview:button];
  89. [_btnArray addObject:button];
  90. }
  91. if (show) {
  92. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  93. cancel.frame = CGRectMake(0, ZCAlertSheetBtnHeight+10+row*ZCAlertSheetBtnHeight+(row+1)*15, KScreenWidth, ZCAlertSheetBtnHeight);
  94. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  95. [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  96. cancel.backgroundColor = [UIColor whiteColor];
  97. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  98. [_alertView addSubview:cancel];
  99. }
  100. }else{
  101. if (show) {
  102. _AllHeight = ZCAlertSheetBtnHeight+10+row*ZCAlertSheetBtnHeight+(row+1)*15;
  103. }else{
  104. _AllHeight = row*ZCAlertSheetBtnHeight+(row+1)*15;
  105. }
  106. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
  107. UIView* btnBkgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, row*ZCAlertSheetBtnHeight+(row+1)*15)];
  108. btnBkgView.backgroundColor = [UIColor whiteColor];
  109. [_alertView addSubview:btnBkgView];
  110. for (int i = 0; i < arr.count; i++) {
  111. ZCAlertActionBtn *action = arr[i];
  112. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15+(i%column)*(btnWidth+15), 15+(i/column)*(15+ZCAlertSheetBtnHeight), btnWidth, ZCAlertSheetBtnHeight)];
  113. button.backgroundColor = HexColorFromRGB(0xb0b0b5);
  114. if(index==i){
  115. button.backgroundColor = HexColorFromRGB(0xfd86a4);
  116. }
  117. button.layer.cornerRadius = 5.0;
  118. [button setTitle:action.title forState:UIControlStateNormal];
  119. [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  120. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  121. button.tag=i;
  122. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  123. [btnBkgView addSubview:button];
  124. [_btnArray addObject:button];
  125. }
  126. if (show) {
  127. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  128. cancel.frame = CGRectMake(0, 10+row*ZCAlertSheetBtnHeight+(row+1)*15, KScreenWidth, ZCAlertSheetBtnHeight);
  129. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  130. [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  131. cancel.backgroundColor = [UIColor whiteColor];
  132. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  133. [_alertView addSubview:cancel];
  134. }
  135. }
  136. }
  137. return self;
  138. }
  139. - (void)youpaiftapClick:(UIButton *)button{
  140. UIButton* oldBtn = _btnArray[_currentSelectIndex];
  141. oldBtn.backgroundColor = LCGray;
  142. button.backgroundColor = ZYPinkColor;
  143. _currentSelectIndex = button.tag;
  144. ZCAlertActionBtn *action = _marr[button.tag];
  145. [self hide];
  146. //加个延时操作防止先回调再hide,影响用户体验
  147. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  148. if (action.block) {
  149. action.block(action.type);
  150. }
  151. });
  152. }
  153. - (void)show{
  154. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
  155. _alertWindow.hidden = NO;
  156. [_alertWindow addSubview:self];
  157. [UIView animateWithDuration:0.3 animations:^{
  158. _alertView.frame = CGRectMake(0, KScreenHeight-_AllHeight, KScreenWidth, _AllHeight);
  159. } completion:^(BOOL finished) {
  160. }];
  161. }
  162. - (void)hide{
  163. [UIView animateWithDuration:0.3 animations:^{
  164. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight);
  165. } completion:^(BOOL finished) {
  166. [self removeFromSuperview];
  167. _alertWindow.hidden = YES;
  168. _alertWindow = nil;
  169. }];
  170. }
  171. @end