BaseAlertView.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // BaseAlertView.m
  3. // 乐销
  4. //
  5. // Created by 隋林栋 on 2017/1/3.
  6. // Copyright © 2017年 ping. All rights reserved.
  7. //
  8. #import "BaseAlertView.h"
  9. @implementation BaseAlertView
  10. #pragma mark 懒加载
  11. - (UIView *)viewBg{
  12. if (_viewBg == nil) {
  13. _viewBg = [UIView new];
  14. _viewBg.backgroundColor = COLOR_BLACK_ALPHA_PER60;
  15. }
  16. return _viewBg;
  17. }
  18. - (UIView *)viewWhite{
  19. if (_viewWhite == nil) {
  20. _viewWhite = [UIView new];
  21. _viewWhite.backgroundColor = [UIColor whiteColor];
  22. [GlobalMethod setRoundView:_viewWhite color:[UIColor clearColor] numRound:5 width:0];
  23. }
  24. return _viewWhite;
  25. }
  26. - (UILabel *)labelAlert{
  27. if (_labelAlert == nil) {
  28. _labelAlert = [UILabel new];
  29. [GlobalMethod setLabel:_labelAlert widthLimit:0 numLines:1 fontNum:F(19) textColor:COLOR_LABEL text:@""];
  30. }
  31. return _labelAlert;
  32. }
  33. - (UILabel *)labeTitle{
  34. if (_labeTitle == nil) {
  35. _labeTitle = [UILabel new];
  36. [GlobalMethod setLabel:_labeTitle widthLimit:0 numLines:0 fontNum:F(15) textColor:COLOR_LABEL text:@""];
  37. }
  38. return _labeTitle;
  39. }
  40. #pragma mark 初始化
  41. - (instancetype)init{
  42. self = [super init];
  43. if (self) {
  44. self.backgroundColor = [UIColor clearColor];
  45. [self addSubview:self.viewBg];
  46. [self addSubview:self.viewWhite];
  47. [self.viewWhite addSubview:self.labelAlert];
  48. [self.viewWhite addSubview:self.labeTitle];
  49. }
  50. return self;
  51. }
  52. #pragma mark 创建
  53. + (instancetype)initWithTitle:(NSString *)title content:(NSString *)content aryBtnModels:(NSArray *)ary viewShow:(UIView *)viewShow{
  54. BaseAlertView * view = [BaseAlertView new];
  55. [view resetWithTitle:title content:content aryBtnModels:ary viewShow:viewShow];
  56. return view;
  57. }
  58. #pragma mark 刷新view
  59. - (void)resetWithTitle:(NSString *)title content:(NSString *)content aryBtnModels:(NSArray *)ary viewShow:(UIView *)viewShow{
  60. self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  61. self.viewBg.frame = CGRectMake(W(0), W(0), SCREEN_WIDTH, SCREEN_HEIGHT);
  62. self.viewWhite.frame = CGRectMake(W(52.5), W(0),SCREEN_WIDTH - W(52.5)*2, W(141));
  63. self.viewWhite.center = CGPointMake(self.viewBg.width/2.0, self.viewBg.height/2.0);
  64. [self.labelAlert fitTitle:title variable:self.viewWhite.width - W(30)];
  65. self.labelAlert.centerXTop = XY(self.viewWhite.width/2.0,W(18));
  66. if (!isStr(content)) {
  67. self.labelAlert.centerY = (self.viewWhite.height - W(50))/2;
  68. }
  69. [self.labeTitle fitTitle:content variable:self.viewWhite.width - W(30)];
  70. self.labeTitle.center = CGPointMake(self.viewWhite.width/2.0, (self.viewWhite.height - self.labelAlert.bottom - W(50))/2.0 + self.labelAlert.bottom);
  71. if (!isStr(title)) {
  72. self.labeTitle.centerY = (self.viewWhite.height - W(50))/2;
  73. }
  74. [self.viewWhite addLineFrame:CGRectMake(0, self.viewWhite.height - W(50)-1, self.viewWhite.width, 1)];
  75. CGFloat widthItem = self.viewWhite.width/ary.count;
  76. for (int i = 0 ; i< ary.count; i++) {
  77. ModelBtn * model = ary[i];
  78. ConBaseAlertView * con = [ConBaseAlertView initWithTitle:model.title labelColor:model.color tag:model.tag frame:CGRectMake(i*widthItem, self.viewWhite.height - W(50), widthItem, W(50)) hasLineVertical:i != ary.count-1];
  79. con.block = model.blockClick;
  80. [con addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  81. [self.viewWhite addSubview:con];
  82. }
  83. [viewShow addSubview:self];
  84. }
  85. #pragma mark 创建
  86. + (void)customAlertControllerWithTitle:(NSString *)title modelBtnArr:(NSArray *)models cancelTitle:(NSString *)cancelTitle cancelColor:(UIColor *)cancelColor selectIndex:(void(^)(int selectIndex))selectIndex{
  87. UIAlertController *alert = [UIAlertController alertControllerWithTitle:isStr(title)?title:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  88. for ( ModelBtn * model in models) {
  89. [self addActionTarget:alert title:UnPackStr(model.title) color:model.color?model.color: [UIColor blackColor] action:^(UIAlertAction *action) {
  90. if (selectIndex) {
  91. selectIndex(model.tag);
  92. }
  93. }];
  94. }
  95. if (isStr(cancelTitle)) {
  96. [self addCancelActionTarget:alert title:cancelTitle titleColor:cancelColor?cancelColor: [UIColor blackColor]];
  97. }
  98. // NSLog(@"%@",GB_Nav.lastVC);
  99. // NSLog(@"%@",GB_Nav.lastVC.presentedViewController);
  100. // UIViewController * hostVC = GB_Nav.lastVC;
  101. // UIViewController * next = hostVC.presentedViewController;
  102. // while (next) {
  103. // hostVC = next;
  104. // next = hostVC.presentedViewController;
  105. // }
  106. [GB_Nav.lastVC presentViewController:alert animated:YES completion:nil];
  107. }
  108. +(void)addActionTarget:(UIAlertController *)alertController title:(NSString *)title color:(UIColor *)color action:(void(^)(UIAlertAction *action))actionTarget
  109. {
  110. UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  111. actionTarget(action);
  112. }];
  113. [action setValue:color forKey:@"_titleTextColor"];
  114. [alertController addAction:action];
  115. }
  116. // 取消按钮
  117. +(void)addCancelActionTarget:(UIAlertController*)alertController title:(NSString *)title titleColor:(UIColor*)titleColor
  118. {
  119. UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  120. }];
  121. [action setValue:titleColor forKey:@"_titleTextColor"];
  122. [alertController addAction:action];
  123. }
  124. #pragma mark 点击事件
  125. - (void)btnClick:(ConBaseAlertView *)sender{
  126. if (sender.block) {
  127. sender.block();
  128. }
  129. [self removeFromSuperview];
  130. }
  131. @end
  132. @implementation ConBaseAlertView
  133. #pragma mark 懒加载
  134. - (UILabel *)label{
  135. if (_label == nil) {
  136. _label = [UILabel new];
  137. [GlobalMethod setLabel:_label widthLimit:0 numLines:0 fontNum:F(17) textColor:COLOR_LABEL text:@""];
  138. }
  139. return _label;
  140. }
  141. #pragma mark 初始化
  142. - (instancetype)init{
  143. self = [super init];
  144. if (self) {
  145. self.backgroundColor = [UIColor clearColor];
  146. [self addSubview:self.label];
  147. }
  148. return self;
  149. }
  150. #pragma mark 创建
  151. + (instancetype)initWithTitle:(NSString *)title labelColor:(UIColor *)labelColor tag:(NSInteger)tag frame:(CGRect)frame hasLineVertical:(BOOL)hasLineVertical{
  152. ConBaseAlertView * view = [ConBaseAlertView new];
  153. [view resetWithTitle:title labelColor:labelColor tag:tag frame:frame hasLineVertical:hasLineVertical];
  154. return view;
  155. }
  156. #pragma mark 刷新view
  157. - (void)resetWithTitle:(NSString *)title labelColor:(UIColor *)labelColor tag:(NSInteger)tag frame:(CGRect)frame hasLineVertical:(BOOL)hasLineVertical{
  158. self.frame = frame;
  159. self.tag = tag;
  160. [self.label fitTitle:title variable:0];
  161. self.label.textColor = labelColor;
  162. self.label.centerXCenterY = XY(self.width/2.0,self.height/2.0);
  163. if (hasLineVertical) {
  164. [self addLineFrame:CGRectMake(self.width - 1, 0, 1, self.height)];
  165. }
  166. }
  167. @end