ComontAlretViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // ComontAlretViewController.m
  3. // 隐私保护
  4. //
  5. // Created by APPLE on 2023/8/30.
  6. //
  7. #import "ComontAlretViewController.h"
  8. #import <Masonry.h>
  9. @interface ComontAlretViewController (){
  10. NSString *titleStr;
  11. NSString *msgStr;
  12. NSString *imageStr;
  13. NSString *cancelTitleStr;
  14. NSString *okTitleStr;
  15. BOOL isOkBtnHighlight;
  16. CommonAlert_ClickOkBtn didClickOkBtn;
  17. CommonAlert_ClickCancelBtn didClickCanaelBtn;
  18. //扩展 按钮倒计时
  19. NSInteger countdownSeconds;
  20. NSTimer *curTimer;
  21. }
  22. @end
  23. @implementation ComontAlretViewController
  24. @synthesize delegate;
  25. - (id)initWithTiTle:(NSString *)title msg:(NSString *)msg imageStr:(NSString *)image cancelTitle:(NSString *)cancelTitle okTitle:(NSString *)okTitle isOkBtnHighlight:(BOOL)okBtnHighlight didClickOk:(CommonAlert_ClickOkBtn)ok didClickCancel:(CommonAlert_ClickCancelBtn)Cancel{
  26. self = [super init];
  27. titleStr = title;
  28. msgStr = msg;
  29. imageStr = image;
  30. cancelTitleStr = cancelTitle;
  31. okTitleStr = okTitle;
  32. isOkBtnHighlight = okBtnHighlight;
  33. didClickOkBtn = ok;
  34. didClickCanaelBtn = Cancel;
  35. return self;
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. [self drawAnyView];
  41. }
  42. - (void)drawAnyView{
  43. [self.view setBackgroundColor:HW000000Color60];
  44. [self.toolBar setHidden:YES];
  45. [self.navigationBar setHidden:YES];
  46. UIView *bgView = [[UIView alloc] init];
  47. [bgView setBackgroundColor:[UIColor whiteColor]];
  48. [bgView.layer setCornerRadius:8.f];
  49. [self.view addSubview:bgView];
  50. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.centerY.equalTo(self.view.mas_centerY).offset(10*HAUTOSCALE);
  52. make.centerX.equalTo(self.view.mas_centerX);
  53. make.width.mas_equalTo(300.f);
  54. }];
  55. CGFloat curTop = 30.0;
  56. //图片
  57. UIImageView *topImageView = [[UIImageView alloc] init];
  58. //topImageView.backgroundColor = [UIColor redColor];
  59. if(imageStr && imageStr.length > 0){
  60. topImageView.image = [UIImage imageNamed:imageStr];
  61. [bgView addSubview:topImageView];
  62. [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.width.mas_equalTo(80.f);
  64. make.height.mas_equalTo(80.f);
  65. make.centerX.equalTo(bgView.mas_centerX);;
  66. make.top.mas_equalTo(curTop);
  67. }];
  68. curTop = 30 + 80 + 10.0;
  69. }
  70. /*主标题*/
  71. UILabel *titleLabel = [[UILabel alloc] init];
  72. [titleLabel setTextColor:HW0A132BColor];
  73. [titleLabel setFont:[UIFont boldSystemFontOfSize:16.f]];
  74. [titleLabel setTextAlignment:(NSTextAlignmentCenter)];
  75. [titleLabel setText:titleStr];
  76. [titleLabel setNumberOfLines:0];
  77. if (titleStr.length > 0){
  78. [bgView addSubview:titleLabel];
  79. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  80. make.left.mas_equalTo(8.f);
  81. make.right.mas_equalTo(-8.f);
  82. make.top.mas_equalTo(curTop);
  83. }];
  84. }
  85. /*副标题*/
  86. UILabel *msgLabel = [[UILabel alloc] init];
  87. [msgLabel setTextColor:HW333333Color];
  88. [msgLabel setFont:[UIFont systemFontOfSize:15.f]];
  89. [msgLabel setTextAlignment:(NSTextAlignmentCenter)];
  90. [msgLabel setText:msgStr];
  91. [msgLabel setNumberOfLines:0];
  92. if (msgStr.length > 0){
  93. [bgView addSubview:msgLabel];
  94. [msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.left.mas_equalTo(8.f);
  96. make.right.mas_equalTo(-8.f);
  97. if (titleStr.length > 0){
  98. make.top.equalTo(titleLabel.mas_bottom).offset(15.f);
  99. }else{
  100. make.top.mas_equalTo(curTop);
  101. }
  102. }];
  103. }
  104. /*双按钮*/
  105. /*取消*/
  106. /*取消按钮*/
  107. UIButton *cancelBtn = [[UIButton alloc] init];
  108. cancelBtn.frame = CGRectMake(0, 0, 115, 48.f);
  109. CGFloat w_btn = 200;
  110. if (okTitleStr.length > 0){
  111. w_btn = 115;
  112. }
  113. // gradient
  114. CAGradientLayer *gl = [CAGradientLayer layer];
  115. gl.frame = CGRectMake(0,0,w_btn,48.f);
  116. gl.startPoint = CGPointMake(0, 0.5);
  117. gl.endPoint = CGPointMake(1, 0.5);
  118. gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
  119. gl.locations = @[@(0), @(1.0f)];
  120. if(!isOkBtnHighlight){
  121. [cancelBtn.layer addSublayer:gl];
  122. [cancelBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  123. }
  124. else{
  125. [cancelBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
  126. [cancelBtn setBackgroundColor:HWE3E8F1Color];
  127. }
  128. [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  129. [cancelBtn setTitle:cancelTitleStr forState:(UIControlStateNormal)];
  130. [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
  131. [cancelBtn.layer setCornerRadius:8.f];
  132. cancelBtn.clipsToBounds = YES;
  133. cancelBtn.tag = 999;
  134. [bgView addSubview:cancelBtn];
  135. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  136. if (okTitleStr.length > 0){
  137. make.left.mas_equalTo(24);
  138. }else{
  139. make.centerX.equalTo(bgView.mas_centerX);
  140. }
  141. if (msgStr.length > 0){
  142. make.top.equalTo(msgLabel.mas_bottom).offset(15.f);
  143. }else{
  144. make.top.equalTo(titleLabel.mas_bottom).offset(25.f);
  145. }
  146. make.width.mas_equalTo(w_btn);
  147. make.bottom.equalTo(bgView.mas_bottom).offset(-30.f);
  148. make.height.mas_equalTo(48.f);
  149. }];
  150. if (okTitleStr.length > 0){
  151. /*残忍拒绝*/
  152. UIButton *okBtn = [[UIButton alloc] init];
  153. if(!isOkBtnHighlight){
  154. [okBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
  155. [okBtn setBackgroundColor:HWE3E8F1Color];
  156. }
  157. else{
  158. [okBtn.layer addSublayer:gl];
  159. [okBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  160. }
  161. [okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  162. [okBtn setTitle:okTitleStr forState:(UIControlStateNormal)];
  163. //[okBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
  164. [okBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
  165. [okBtn.layer setCornerRadius:8.f];
  166. //[okBtn setBackgroundColor:HWE3E8F1Color];
  167. okBtn.clipsToBounds = YES;
  168. [bgView addSubview:okBtn];
  169. [okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  170. make.right.mas_equalTo(-24.f);
  171. make.width.mas_equalTo(w_btn);
  172. make.top.equalTo(cancelBtn.mas_top);
  173. make.height.mas_equalTo(48.f);
  174. }];
  175. }
  176. }
  177. //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  178. // [self dismissViewControllerAnimated:YES completion:^{
  179. // ;
  180. // }];
  181. //}
  182. - (void)cancelBtnClick{
  183. [self dismissViewControllerAnimated:YES completion:^{
  184. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  185. if ([self->delegate respondsToSelector:@selector(CommonAlertCancelBtnClickPressed)]){
  186. [self->delegate CommonAlertCancelBtnClickPressed];
  187. }
  188. if(self->didClickCanaelBtn){
  189. self->didClickCanaelBtn();
  190. }
  191. });
  192. }];
  193. }
  194. - (void)okBtnClick{
  195. [self dismissViewControllerAnimated:YES completion:^{
  196. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  197. if ([self->delegate respondsToSelector:@selector(CommonAlertokBtnClickPressed)]){
  198. [self->delegate CommonAlertokBtnClickPressed];
  199. }
  200. if(self->didClickOkBtn){
  201. self->didClickOkBtn();
  202. }
  203. });
  204. }];
  205. }
  206. - (void)setButtonCountdownFun:(NSInteger)second
  207. {
  208. UIButton *but = [self.view viewWithTag:999];
  209. if(but && [but isKindOfClass:[UIButton class]]){
  210. countdownSeconds = second;
  211. [but setEnabled:NO];
  212. but.alpha = 0.5;
  213. NSString *curTitle = cancelTitleStr;
  214. curTitle = [[NSString alloc] initWithFormat:@"%@(%ld)",curTitle,countdownSeconds];
  215. [but setTitle:curTitle forState:UIControlStateNormal];
  216. [curTimer invalidate];
  217. curTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleCountdownFun) userInfo:nil repeats:YES];
  218. }
  219. }
  220. - (void)handleCountdownFun
  221. {
  222. if(countdownSeconds >= 1){
  223. countdownSeconds --;
  224. UIButton *but = [self.view viewWithTag:999];
  225. NSString *curTitle = cancelTitleStr;
  226. curTitle = [[NSString alloc] initWithFormat:@"%@(%ld)",curTitle,countdownSeconds];
  227. [but setTitle:curTitle forState:UIControlStateNormal];
  228. }
  229. else{
  230. [curTimer invalidate];
  231. [self dismissViewControllerAnimated:NO completion:^{
  232. if ([self->delegate respondsToSelector:@selector(CommonAlertokBtnClickPressed)]){
  233. [self->delegate CommonAlertokBtnClickPressed];
  234. }
  235. if(self->didClickOkBtn){
  236. self->didClickOkBtn();
  237. }
  238. }];
  239. }
  240. }
  241. /*
  242. #pragma mark - Navigation
  243. // In a storyboard-based application, you will often want to do a little preparation before navigation
  244. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  245. // Get the new view controller using [segue destinationViewController].
  246. // Pass the selected object to the new view controller.
  247. }
  248. */
  249. @end