ComontAlretViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. bgView.tag = 999;
  50. [self.view addSubview:bgView];
  51. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.centerY.equalTo(self.view.mas_centerY).offset(10*HAUTOSCALE);
  53. make.centerX.equalTo(self.view.mas_centerX);
  54. make.width.mas_equalTo(300.f);
  55. }];
  56. CGFloat curTop = 30.0;
  57. //图片
  58. UIImageView *topImageView = [[UIImageView alloc] init];
  59. //topImageView.backgroundColor = [UIColor redColor];
  60. if(imageStr && imageStr.length > 0){
  61. topImageView.image = [UIImage imageNamed:imageStr];
  62. [bgView addSubview:topImageView];
  63. [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.width.mas_equalTo(80.f);
  65. make.height.mas_equalTo(80.f);
  66. make.centerX.equalTo(bgView.mas_centerX);;
  67. make.top.mas_equalTo(curTop);
  68. }];
  69. curTop = 30 + 80 + 10.0;
  70. }
  71. /*主标题*/
  72. UILabel *titleLabel = [[UILabel alloc] init];
  73. [titleLabel setTextColor:HW0A132BColor];
  74. [titleLabel setFont:[UIFont boldSystemFontOfSize:16.f]];
  75. [titleLabel setTextAlignment:(NSTextAlignmentCenter)];
  76. [titleLabel setText:titleStr];
  77. [titleLabel setNumberOfLines:0];
  78. if (titleStr.length > 0){
  79. [bgView addSubview:titleLabel];
  80. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.left.mas_equalTo(20.f);
  82. make.right.mas_equalTo(-20.f);
  83. make.top.mas_equalTo(curTop);
  84. }];
  85. }
  86. /*副标题*/
  87. UILabel *msgLabel = [[UILabel alloc] init];
  88. [msgLabel setTextColor:HW333333Color];
  89. [msgLabel setFont:[UIFont systemFontOfSize:15.f]];
  90. [msgLabel setTextAlignment:(NSTextAlignmentCenter)];
  91. msgLabel.tag = 11;
  92. [msgLabel setText:msgStr];
  93. [msgLabel setNumberOfLines:0];
  94. if (msgStr.length > 0){
  95. [bgView addSubview:msgLabel];
  96. [msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.left.mas_equalTo(20.f);
  98. make.right.mas_equalTo(-20.f);
  99. if (titleStr.length > 0){
  100. make.top.equalTo(titleLabel.mas_bottom).offset(15.f);
  101. }else{
  102. make.top.mas_equalTo(curTop);
  103. }
  104. }];
  105. }
  106. /*双按钮*/
  107. /*取消*/
  108. /*取消按钮*/
  109. UIButton *cancelBtn = [[UIButton alloc] init];
  110. cancelBtn.frame = CGRectMake(0, 0, 115, 48.f);
  111. CGFloat w_btn = 200;
  112. if (okTitleStr.length > 0){
  113. w_btn = 115;
  114. }
  115. // gradient
  116. CAGradientLayer *gl = [CAGradientLayer layer];
  117. gl.frame = CGRectMake(0,0,w_btn,48.f);
  118. gl.startPoint = CGPointMake(0, 0.5);
  119. gl.endPoint = CGPointMake(1, 0.5);
  120. gl.colors = @[(__bridge id)HW0CDEFDColor.CGColor, (__bridge id)HW058DFBColor.CGColor];
  121. gl.locations = @[@(0), @(1.0f)];
  122. if(!isOkBtnHighlight){
  123. [cancelBtn.layer addSublayer:gl];
  124. [cancelBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  125. }
  126. else{
  127. [cancelBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
  128. [cancelBtn setBackgroundColor:HWE3E8F1Color];
  129. }
  130. [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  131. [cancelBtn setTitle:cancelTitleStr forState:(UIControlStateNormal)];
  132. [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
  133. [cancelBtn.layer setCornerRadius:8.f];
  134. cancelBtn.clipsToBounds = YES;
  135. cancelBtn.tag = 999;
  136. [bgView addSubview:cancelBtn];
  137. [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  138. if (okTitleStr.length > 0){
  139. make.left.mas_equalTo(24);
  140. }else{
  141. make.centerX.equalTo(bgView.mas_centerX);
  142. }
  143. if (msgStr.length > 0){
  144. make.top.equalTo(msgLabel.mas_bottom).offset(15.f);
  145. }else{
  146. make.top.equalTo(titleLabel.mas_bottom).offset(25.f);
  147. }
  148. make.width.mas_equalTo(w_btn);
  149. make.bottom.equalTo(bgView.mas_bottom).offset(-30.f);
  150. make.height.mas_equalTo(48.f);
  151. }];
  152. if (okTitleStr.length > 0){
  153. /*残忍拒绝*/
  154. UIButton *okBtn = [[UIButton alloc] init];
  155. if(!isOkBtnHighlight){
  156. [okBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
  157. [okBtn setBackgroundColor:HWE3E8F1Color];
  158. }
  159. else{
  160. [okBtn.layer addSublayer:gl];
  161. [okBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
  162. }
  163. [okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
  164. [okBtn setTitle:okTitleStr forState:(UIControlStateNormal)];
  165. //[okBtn setTitleColor:HW0A132BColor forState:(UIControlStateNormal)];
  166. [okBtn.titleLabel setFont:[UIFont systemFontOfSize:16.f]];
  167. [okBtn.layer setCornerRadius:8.f];
  168. //[okBtn setBackgroundColor:HWE3E8F1Color];
  169. okBtn.clipsToBounds = YES;
  170. [bgView addSubview:okBtn];
  171. [okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  172. make.right.mas_equalTo(-24.f);
  173. make.width.mas_equalTo(w_btn);
  174. make.top.equalTo(cancelBtn.mas_top);
  175. make.height.mas_equalTo(48.f);
  176. }];
  177. }
  178. }
  179. //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  180. // [self dismissViewControllerAnimated:YES completion:^{
  181. // ;
  182. // }];
  183. //}
  184. - (void)cancelBtnClick{
  185. if(curTimer)[curTimer invalidate];
  186. [self dismissViewControllerAnimated:YES completion:^{
  187. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  188. if ([self->delegate respondsToSelector:@selector(CommonAlertCancelBtnClickPressed)]){
  189. [self->delegate CommonAlertCancelBtnClickPressed];
  190. }
  191. if(self->didClickCanaelBtn){
  192. self->didClickCanaelBtn();
  193. }
  194. });
  195. }];
  196. }
  197. - (void)okBtnClick{
  198. if(curTimer)[curTimer invalidate];
  199. [self dismissViewControllerAnimated:YES completion:^{
  200. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  201. if ([self->delegate respondsToSelector:@selector(CommonAlertokBtnClickPressed)]){
  202. [self->delegate CommonAlertokBtnClickPressed];
  203. }
  204. if(self->didClickOkBtn){
  205. self->didClickOkBtn();
  206. }
  207. });
  208. }];
  209. }
  210. - (void)setButtonCountdownFun:(NSInteger)second
  211. {
  212. UIButton *but = [self.view viewWithTag:999];
  213. if(but && [but isKindOfClass:[UIButton class]]){
  214. countdownSeconds = second;
  215. [but setEnabled:NO];
  216. but.alpha = 0.5;
  217. NSString *curTitle = cancelTitleStr;
  218. curTitle = [[NSString alloc] initWithFormat:@"%@(%ld)",curTitle,countdownSeconds];
  219. [but setTitle:curTitle forState:UIControlStateNormal];
  220. [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  221. [[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
  222. }];
  223. [curTimer invalidate];
  224. curTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleCountdownFun) userInfo:nil repeats:YES];
  225. [[NSRunLoop mainRunLoop] addTimer:curTimer forMode:NSRunLoopCommonModes];
  226. }
  227. }
  228. - (void)handleCountdownFun
  229. {
  230. if(countdownSeconds >= 1){
  231. countdownSeconds --;
  232. UIButton *but = [self.view viewWithTag:999];
  233. NSString *curTitle = cancelTitleStr;
  234. curTitle = [[NSString alloc] initWithFormat:@"%@(%ld)",curTitle,countdownSeconds];
  235. [but setTitle:curTitle forState:UIControlStateNormal];
  236. }
  237. else{
  238. [curTimer invalidate];
  239. [self dismissViewControllerAnimated:NO completion:^{
  240. if ([self->delegate respondsToSelector:@selector(CommonAlertokBtnClickPressed)]){
  241. [self->delegate CommonAlertokBtnClickPressed];
  242. }
  243. if(self->didClickOkBtn){
  244. self->didClickOkBtn();
  245. }
  246. }];
  247. }
  248. }
  249. - (void)setMsgTextAlignment:(NSTextAlignment)textAlignment
  250. {
  251. UILabel *label = [self.view viewWithTag:11];
  252. if(label && [label isKindOfClass:[UILabel class]]){
  253. label.textAlignment = textAlignment;
  254. }
  255. }
  256. /*
  257. #pragma mark - Navigation
  258. // In a storyboard-based application, you will often want to do a little preparation before navigation
  259. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  260. // Get the new view controller using [segue destinationViewController].
  261. // Pass the selected object to the new view controller.
  262. }
  263. */
  264. @end