SPSBConfirmAlertViewController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // SPSBConfirmAlertViewController.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/26.
  6. //
  7. #import "SPSBConfirmAlertViewController.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. @interface SPSBConfirmAlertViewController () {
  10. NSString *_title;
  11. NSString *_content;
  12. UIView *_alertView;
  13. UIButton *_cancelButton;
  14. UIButton *_confirmButton;
  15. void(^_Nullable _cancelAction)(void);
  16. void(^_Nullable _confirmAction)(void);
  17. }
  18. @end
  19. @implementation SPSBConfirmAlertViewController
  20. + (instancetype)alertWithTitle:(nullable NSString *)title content:(nullable NSString *)content {
  21. SPSBConfirmAlertViewController *alert = SPSBConfirmAlertViewController.new;
  22. alert->_title = title;
  23. alert->_content = content;
  24. return alert;
  25. }
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. [self setupUI];
  29. }
  30. #pragma mark - Action
  31. - (void)confirmAction {
  32. [self closeActionCompletion:^{
  33. if (self->_confirmAction) {
  34. self->_confirmAction();
  35. }
  36. }];
  37. }
  38. - (void)cancelAction {
  39. [self closeActionCompletion:^{
  40. if (self->_cancelAction) {
  41. self->_cancelAction();
  42. }
  43. }];
  44. }
  45. - (void)closeActionCompletion: (void (^_Nullable)(void))completion {
  46. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  47. animation.fromValue = [NSNumber numberWithFloat:1.0];
  48. animation.toValue = [NSNumber numberWithFloat:0.8];
  49. animation.duration = 0.25;
  50. animation.removedOnCompletion = true;
  51. [_alertView.layer addAnimation:animation forKey:nil];
  52. [UIView animateWithDuration:0.25 animations:^{
  53. self->_alertView.alpha = 0.f;
  54. self.view.backgroundColor =spsb_000000_color(0.f);
  55. } completion:^(BOOL finished) {
  56. [self dismissViewControllerAnimated:false completion:completion];
  57. }];
  58. }
  59. - (void)showAnimation {
  60. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  61. animation.fromValue = [NSNumber numberWithFloat:1.2];
  62. animation.toValue = [NSNumber numberWithFloat:1.0];
  63. animation.duration = 0.25;
  64. animation.removedOnCompletion = true;
  65. [_alertView.layer addAnimation:animation forKey:nil];
  66. [UIView animateWithDuration:0.25 animations:^{
  67. self.view.backgroundColor = spsb_000000_color(SPSBPopupViewBackgroundAlpha);
  68. self->_alertView.alpha = 1.f;
  69. }];
  70. }
  71. - (void)setupAnimation {
  72. self.view.backgroundColor = spsb_000000_color(0.f);
  73. }
  74. #pragma mark - UI
  75. - (void)setupUI {
  76. [self createAlert];
  77. }
  78. #define byh_widthLimit 253
  79. - (void)setCancelButtonTitle:(nullable NSString *)title titleColor:(nullable UIColor *)color action:(void(^_Nullable)(void))action {
  80. _cancelButton = [UIButton convenienceWithFont:spsb_font(18) target:self action:@selector(cancelAction)];
  81. [_cancelButton setTitle:title ?: @"取消" titleColor:color ?: spsb_3296FB_color() image:nil backgroundImage:jxh_createImage(spsb_FFFFFF_color(1.f), (CGSize){50, 50}) state:JXHButtonControlStateNormal];
  82. [_cancelButton setBackgroundImage:jxh_createImage(spsb_000000_color(0.1), (CGSize){50, 50}) state:JXHButtonControlStateHighlighted];
  83. _cancelAction = action;
  84. }
  85. - (void)setConfirmButtonTitle:(nullable NSString *)title titleColor:(nullable UIColor *)color action:(void(^_Nullable)(void))action {
  86. _confirmButton = [UIButton convenienceWithFont:spsb_mediumFont(18) target:self action:@selector(confirmAction)];
  87. [_confirmButton setTitle:title ?: @"确定" titleColor:color ?: spsb_3296FB_color() image:nil backgroundImage:jxh_createImage(spsb_FFFFFF_color(1.f), (CGSize){50, 50}) state:JXHButtonControlStateNormal];
  88. [_confirmButton setBackgroundImage:jxh_createImage(spsb_000000_color(0.1), (CGSize){50, 50}) state:JXHButtonControlStateHighlighted];
  89. _confirmAction = action;
  90. }
  91. - (void)createAlert {
  92. _alertView = UIView.new;
  93. _alertView.backgroundColor = spsb_FFFFFF_color(1.f);
  94. _alertView.alpha = 0.f;
  95. [_alertView setLayerCornerRadius:14 clipToBounds:true];
  96. [self.view addSubview:_alertView];
  97. [_alertView makeConstraints:^(JXHConstraintMaker *make) {
  98. make.center.equalTo(self.view);
  99. make.width.equalTo(313);
  100. }];
  101. UIView *contentView = UIView.new;
  102. [_alertView addSubview:contentView];
  103. [contentView makeConstraints:^(JXHConstraintMaker *make) {
  104. make.top.and.leading.and.trailing.equalTo(0);
  105. make.height.greaterThanOrEqualTo(60).priorityRequired();
  106. }];
  107. NSString *t = _title ?: @"";
  108. UILabel *title = [UILabel convenienceWithFont:spsb_mediumFont(18) textColor:spsb_333333_color() limitWidth:byh_widthLimit];
  109. [title setAttributedStringWithText:t textAlignment:NSTextAlignmentCenter lineSpacing:3 lineHeight:0 paragraphSpacing:0 lineBreakMode:0 lines:jxh_getLinesNum(t, byh_widthLimit, spsb_mediumFont(18))];
  110. [title setWordKern:-0.53];
  111. [contentView addSubview:title];
  112. NSString *c = _content ?: @"";
  113. UILabel *content = [UILabel convenienceWithFont:spsb_font(16) textColor:spsb_666666_color() limitWidth:byh_widthLimit];
  114. [content setAttributedStringWithText:c textAlignment:NSTextAlignmentCenter lineSpacing:5 lineHeight:0 paragraphSpacing:0 lineBreakMode:0 lines:jxh_getLinesNum(c, byh_widthLimit, spsb_font(16))];
  115. [contentView addSubview:content];
  116. if (_title && _content) {
  117. [title makeConstraints:^(JXHConstraintMaker *make) {
  118. make.centerX.equalTo(contentView);
  119. make.top.equalTo(28);
  120. make.height.lessThanOrEqualTo(jxh_screenHeight() / 3).priorityRequired();
  121. }];
  122. [content makeConstraints:^(JXHConstraintMaker *make) {
  123. make.centerX.equalTo(contentView);
  124. make.top.equalTo(title.bottom).offset(8);
  125. make.bottom.equalTo(-28);
  126. make.height.lessThanOrEqualTo(jxh_screenHeight() / 3).priorityRequired();
  127. }];
  128. } else if (_title) {
  129. [title makeConstraints:^(JXHConstraintMaker *make) {
  130. make.centerX.equalTo(contentView);
  131. make.centerY.equalTo(contentView).priorityRequired();
  132. make.top.greaterThanOrEqualTo(28);
  133. make.bottom.lessThanOrEqualTo(-28);
  134. make.height.lessThanOrEqualTo(jxh_screenHeight() / 2).priorityRequired();
  135. }];
  136. } else if (_content) {
  137. [content makeConstraints:^(JXHConstraintMaker *make) {
  138. make.centerX.equalTo(contentView);
  139. make.centerY.equalTo(contentView).priorityRequired();
  140. make.top.greaterThanOrEqualTo(28);
  141. make.bottom.lessThanOrEqualTo(-28);
  142. make.height.lessThanOrEqualTo(jxh_screenHeight() / 2).priorityRequired();
  143. }];
  144. }
  145. [contentView createLineWithLocation:JXHLineLocationBottom];
  146. UIView *buttonView = UIView.new;
  147. [_alertView addSubview:buttonView];
  148. [buttonView makeConstraints:^(JXHConstraintMaker *make) {
  149. make.bottom.and.leading.and.trailing.equalTo(0);
  150. make.top.equalTo(contentView.bottom);
  151. make.height.equalTo(50);
  152. }];
  153. if (!_confirmButton && !_cancelButton) {
  154. [self setConfirmButtonTitle:nil titleColor:nil action:nil];
  155. }
  156. if (_confirmButton && _cancelButton) {
  157. [buttonView addSubview:_cancelButton];
  158. [_cancelButton makeConstraints:^(JXHConstraintMaker *make) {
  159. make.top.and.bottom.and.leading.equalTo(0);
  160. }];
  161. [buttonView addSubview:_confirmButton];
  162. [_confirmButton makeConstraints:^(JXHConstraintMaker *make) {
  163. make.top.and.bottom.and.trailing.equalTo(0);
  164. make.leading.equalTo(self->_cancelButton.trailing);
  165. make.width.equalTo(self->_cancelButton);
  166. }];
  167. [buttonView createLineWithLocation:JXHLineLocationHorizontalCenter];
  168. } else if (_confirmButton) {
  169. [buttonView addSubview:_confirmButton];
  170. [_confirmButton makeConstraints:^(JXHConstraintMaker *make) {
  171. make.edges.equalTo(buttonView);
  172. }];
  173. } else {
  174. [buttonView addSubview:_cancelButton];
  175. [_cancelButton makeConstraints:^(JXHConstraintMaker *make) {
  176. make.edges.equalTo(buttonView);
  177. }];
  178. }
  179. }
  180. @end