ComontAlretViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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(10.f);
  81. make.right.mas_equalTo(-10.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(10.f);
  97. make.right.mas_equalTo(-10.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. [self dismissViewControllerAnimated:YES completion:^{
  185. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  186. if ([self->delegate respondsToSelector:@selector(CommonAlertCancelBtnClickPressed)]){
  187. [self->delegate CommonAlertCancelBtnClickPressed];
  188. }
  189. if(self->didClickCanaelBtn){
  190. self->didClickCanaelBtn();
  191. }
  192. });
  193. }];
  194. }
  195. - (void)okBtnClick{
  196. [self dismissViewControllerAnimated:YES completion:^{
  197. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  198. if ([self->delegate respondsToSelector:@selector(CommonAlertokBtnClickPressed)]){
  199. [self->delegate CommonAlertokBtnClickPressed];
  200. }
  201. if(self->didClickOkBtn){
  202. self->didClickOkBtn();
  203. }
  204. });
  205. }];
  206. }
  207. - (void)setButtonCountdownFun:(NSInteger)second
  208. {
  209. UIButton *but = [self.view viewWithTag:999];
  210. if(but && [but isKindOfClass:[UIButton class]]){
  211. countdownSeconds = second;
  212. [but setEnabled:NO];
  213. but.alpha = 0.5;
  214. NSString *curTitle = cancelTitleStr;
  215. curTitle = [[NSString alloc] initWithFormat:@"%@(%ld)",curTitle,countdownSeconds];
  216. [but setTitle:curTitle forState:UIControlStateNormal];
  217. [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  218. [[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
  219. }];
  220. [curTimer invalidate];
  221. curTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleCountdownFun) userInfo:nil repeats:YES];
  222. [[NSRunLoop mainRunLoop] addTimer:curTimer forMode:NSRunLoopCommonModes];
  223. }
  224. }
  225. - (void)handleCountdownFun
  226. {
  227. if(countdownSeconds >= 1){
  228. countdownSeconds --;
  229. UIButton *but = [self.view viewWithTag:999];
  230. NSString *curTitle = cancelTitleStr;
  231. curTitle = [[NSString alloc] initWithFormat:@"%@(%ld)",curTitle,countdownSeconds];
  232. [but setTitle:curTitle forState:UIControlStateNormal];
  233. }
  234. else{
  235. [curTimer invalidate];
  236. [self dismissViewControllerAnimated:NO completion:^{
  237. if ([self->delegate respondsToSelector:@selector(CommonAlertokBtnClickPressed)]){
  238. [self->delegate CommonAlertokBtnClickPressed];
  239. }
  240. if(self->didClickOkBtn){
  241. self->didClickOkBtn();
  242. }
  243. }];
  244. }
  245. }
  246. - (void)setMsgTextAlignment:(NSTextAlignment)textAlignment
  247. {
  248. UILabel *label = [self.view viewWithTag:11];
  249. if(label && [label isKindOfClass:[UILabel class]]){
  250. label.textAlignment = textAlignment;
  251. }
  252. }
  253. /*
  254. #pragma mark - Navigation
  255. // In a storyboard-based application, you will often want to do a little preparation before navigation
  256. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  257. // Get the new view controller using [segue destinationViewController].
  258. // Pass the selected object to the new view controller.
  259. }
  260. */
  261. @end