ZCAlertSheetView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // JWAlertSheetView.m
  3. // JWlive
  4. //
  5. // Created by 蒋理智 on 16/3/25.
  6. // Copyright © 2016年 pican zhang. All rights reserved.
  7. //
  8. #import "ZCAlertSheetView.h"
  9. #define ZCAlertSheetHeight (49*KScreenWidth/320)//根据比例得到高度
  10. @implementation ZCAlertAction
  11. + (ZCAlertAction *)actionWithTitle:(NSString *)title andblock:(handleBlock)block{
  12. ZCAlertAction *action = [[ZCAlertAction alloc]initWithTitle:title andblock:block];
  13. return action;
  14. }
  15. - (id)initWithTitle:(NSString *)title andblock:(handleBlock)block{
  16. if (self = [super init]) {
  17. self.block = block;
  18. self.title = title;
  19. }
  20. return self;
  21. }
  22. @end
  23. @interface ZCAlertSheetView ()
  24. {
  25. UIView *_alertView;
  26. CGFloat _AllHeight;
  27. NSMutableArray *_marr;
  28. }
  29. @end
  30. @implementation ZCAlertSheetView
  31. - (UIWindow*)alertWindow{
  32. if (!_alertWindow) {
  33. //modify by leo --fix bug 20191008
  34. //_alertWindow = [[UIApplication sharedApplication]keyWindow];
  35. _alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
  36. _alertWindow.windowLevel = UIWindowLevelStatusBar+1;
  37. }
  38. return _alertWindow;
  39. }
  40. - (id)initWithTitle:(NSString *)title andShowCancelButton:(BOOL)show andAction:(NSArray<ZCAlertAction *> *)arr{
  41. if (self = [super init]) {
  42. self.frame = [UIScreen mainScreen].bounds;
  43. _marr = [NSMutableArray arrayWithArray:arr];
  44. UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  45. [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  46. control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5];
  47. // control.alpha = 0.5;
  48. [self addSubview:control];
  49. _alertView = [[UIView alloc]init];
  50. _alertView.backgroundColor = HexColorFromRGB(0xffffff);
  51. [self addSubview:_alertView];
  52. if (title) {
  53. if (show) {
  54. _AllHeight = ZCAlertSheetHeight*(arr.count+1)+ZCAlertSheetHeight+10;
  55. }else{
  56. _AllHeight = ZCAlertSheetHeight*(arr.count+1);
  57. }
  58. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight);
  59. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCAlertSheetHeight-1)];
  60. titleLabel.text = title;
  61. titleLabel.textAlignment = NSTextAlignmentCenter;
  62. titleLabel.font = [UIFont systemFontOfSize:16];
  63. titleLabel.textColor = LZ273145Color;
  64. [_alertView addSubview:titleLabel];
  65. for (int i = 0; i < arr.count; i++) {
  66. ZCAlertAction *action = arr[i];
  67. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight+ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)];
  68. button.backgroundColor = LCWhiteColor;
  69. [button setTitle:action.title forState:UIControlStateNormal];
  70. [button setTitleColor:LZ273145Color forState:UIControlStateNormal];
  71. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  72. button.tag=i;
  73. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  74. [_alertView addSubview:button];
  75. }
  76. if (show) {
  77. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  78. cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count+1)+10, KScreenWidth, ZCAlertSheetHeight);
  79. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  80. [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  81. cancel.backgroundColor = LCWhiteColor;
  82. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  83. [_alertView addSubview:cancel];
  84. }
  85. }else{
  86. if (show) {
  87. _AllHeight = ZCAlertSheetHeight*(arr.count)+ZCAlertSheetHeight+10;
  88. }else{
  89. _AllHeight = ZCAlertSheetHeight*(arr.count);
  90. }
  91. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight);
  92. for (int i = 0; i < arr.count; i++) {
  93. ZCAlertAction *action = arr[i];
  94. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)];
  95. button.backgroundColor = LCWhiteColor;
  96. [button setTitle:action.title forState:UIControlStateNormal];
  97. [button setTitleColor:LZ273145Color forState:UIControlStateNormal];
  98. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  99. button.tag=i;
  100. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  101. [_alertView addSubview:button];
  102. [button addLineWithColor:[HexColorFromRGB(0xffffff) colorWithAlphaComponent:0.29f] lineRect:CGRectMake(14.0f, ZCAlertSheetHeight-1 - 0.5f, KScreenWidth - 28.0f, 0.5f)];
  103. }
  104. if (show) {
  105. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  106. cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count)+10, KScreenWidth, ZCAlertSheetHeight);
  107. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  108. [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  109. cancel.backgroundColor = LCWhiteColor;
  110. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  111. [_alertView addSubview:cancel];
  112. }
  113. }
  114. CAShapeLayer *sectionmaskLayer = [CAShapeLayer layer];
  115. UIBezierPath *sectionpath = [UIBezierPath bezierPathWithRoundedRect:_alertView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
  116. sectionmaskLayer.path = sectionpath.CGPath;
  117. _alertView.layer.mask = sectionmaskLayer;
  118. }
  119. return self;
  120. }
  121. - (id)initWithAttrbuteTitle:(NSAttributedString *)attrbuteTitle andShowCancelButton:(BOOL)show andAction:(NSArray<ZCAlertAction *> *)arr{
  122. if (self = [super init]) {
  123. self.frame = [UIScreen mainScreen].bounds;
  124. _marr = [NSMutableArray arrayWithArray:arr];
  125. UIControl *control = [[UIControl alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  126. [control addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  127. control.backgroundColor = [HexColorFromRGB(0x101010) colorWithAlphaComponent:0.5];
  128. // control.alpha = 0.5;
  129. [self addSubview:control];
  130. _alertView = [[UIView alloc]init];
  131. _alertView.backgroundColor = LCWhiteColor;
  132. [self addSubview:_alertView];
  133. if (attrbuteTitle) {
  134. if (show) {
  135. _AllHeight = ZCAlertSheetHeight*(arr.count+1)+ZCAlertSheetHeight+10;
  136. }else{
  137. _AllHeight = ZCAlertSheetHeight*(arr.count+1);
  138. }
  139. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight);
  140. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ZCAlertSheetHeight-1)];
  141. titleLabel.textAlignment = NSTextAlignmentCenter;
  142. titleLabel.font = [UIFont systemFontOfSize:15];
  143. titleLabel.textColor = HexColorFromRGB(0x9F9DA5);
  144. titleLabel.attributedText = attrbuteTitle;
  145. [_alertView addSubview:titleLabel];
  146. for (int i = 0; i < arr.count; i++) {
  147. ZCAlertAction *action = arr[i];
  148. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight+ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)];
  149. button.backgroundColor = LCWhiteColor;
  150. [button setTitle:action.title forState:UIControlStateNormal];
  151. [button setTitleColor:LZ273145Color forState:UIControlStateNormal];
  152. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  153. button.tag=i;
  154. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  155. [_alertView addSubview:button];
  156. }
  157. if (show) {
  158. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  159. cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count+1)+10, KScreenWidth, ZCAlertSheetHeight);
  160. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  161. [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  162. cancel.backgroundColor = LCWhiteColor;
  163. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  164. [_alertView addSubview:cancel];
  165. }
  166. }else{
  167. if (show) {
  168. _AllHeight = ZCAlertSheetHeight*(arr.count)+ZCAlertSheetHeight+10;
  169. }else{
  170. _AllHeight = ZCAlertSheetHeight*(arr.count);
  171. }
  172. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight + SafeHeight);
  173. for (int i = 0; i < arr.count; i++) {
  174. ZCAlertAction *action = arr[i];
  175. UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, ZCAlertSheetHeight*i, self.frame.size.width, ZCAlertSheetHeight-1)];
  176. button.backgroundColor = LCWhiteColor;
  177. [button setTitle:action.title forState:UIControlStateNormal];
  178. [button setTitleColor:LZ273145Color forState:UIControlStateNormal];
  179. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  180. button.tag=i;
  181. [button addTarget:self action:@selector(youpaiftapClick:) forControlEvents:UIControlEventTouchUpInside];
  182. [_alertView addSubview:button];
  183. [button addLineWithColor:[HexColorFromRGB(0xffffff) colorWithAlphaComponent:0.29f] lineRect:CGRectMake(14.0f, ZCAlertSheetHeight-1 - 0.5f, KScreenWidth - 28.0f, 0.5f)];
  184. }
  185. if (show) {
  186. UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
  187. cancel.frame = CGRectMake(0, ZCAlertSheetHeight*(arr.count)+10, KScreenWidth, ZCAlertSheetHeight);
  188. [cancel setTitle:@"取消" forState:UIControlStateNormal];
  189. [cancel setTitleColor:LZA3AABEColor forState:UIControlStateNormal];
  190. cancel.backgroundColor = LCWhiteColor;
  191. [cancel addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
  192. [_alertView addSubview:cancel];
  193. }
  194. }
  195. CAShapeLayer *sectionmaskLayer = [CAShapeLayer layer];
  196. UIBezierPath *sectionpath = [UIBezierPath bezierPathWithRoundedRect:_alertView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
  197. sectionmaskLayer.path = sectionpath.CGPath;
  198. _alertView.layer.mask = sectionmaskLayer;
  199. }
  200. return self;
  201. }
  202. - (void)youpaiftapClick:(UIButton *)button{
  203. ZCAlertAction *action = _marr[button.tag];
  204. [self hide];
  205. //加个延时操作防止先回调再hide,影响用户体验
  206. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  207. if (action.block) {
  208. action.block();
  209. }
  210. });
  211. }
  212. - (void)show{
  213. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
  214. _alertWindow.hidden = NO;
  215. [_alertWindow addSubview:self];
  216. [UIView animateWithDuration:0.3 animations:^{
  217. _alertView.frame = CGRectMake(0, KScreenHeight-_AllHeight-SafeHeight, KScreenWidth, _AllHeight+SafeHeight);
  218. } completion:^(BOOL finished) {
  219. }];
  220. }
  221. - (void)hide{
  222. [UIView animateWithDuration:0.3 animations:^{
  223. _alertView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, _AllHeight+SafeHeight);
  224. } completion:^(BOOL finished) {
  225. [self removeFromSuperview];
  226. _alertWindow.hidden = YES;
  227. _alertWindow = nil;
  228. }];
  229. }
  230. @end