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