MOSystemAlertView.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // MOSystemAlertView.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/9/17.
  6. //
  7. #define kContentViewWidth kScaleWidth(295)
  8. #import "MOSystemAlertView.h"
  9. @interface MOSystemAlertView()
  10. @property (nonatomic, strong) UIView *bgView;
  11. @property (nonatomic, strong) UIView *contentView;
  12. @property (nonatomic, strong) UILabel *titleLabel;
  13. @property (nonatomic, strong) UILabel *contentLabel;
  14. @property (nonatomic, strong) UIView *hLineView;//横线
  15. @property (nonatomic, strong) UIView *vLineView;//竖线
  16. @property (nonatomic, strong) UIButton *cancelButton;
  17. @property (nonatomic, strong) UIButton *confirmButton;
  18. @end
  19. @implementation MOSystemAlertView
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. [self setupUI];
  23. }
  24. return self;
  25. }
  26. - (void)setupUI {
  27. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  28. [keyWindow addSubview:self.bgView];
  29. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  30. make.top.left.bottom.right.equalTo(keyWindow);
  31. }];
  32. [keyWindow addSubview:self.contentView];
  33. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.width.mas_equalTo(kContentViewWidth);
  35. make.center.mas_equalTo(0);
  36. make.height.mas_equalTo(116);
  37. }];
  38. [self.contentView addSubview:self.titleLabel];
  39. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.top.mas_equalTo(24);
  41. make.centerX.mas_equalTo(0);
  42. make.left.mas_equalTo(20);
  43. make.right.mas_equalTo(-20);
  44. }];
  45. [self.contentView addSubview:self.contentLabel];
  46. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.top.equalTo(self.titleLabel.mas_bottom).offset(8);//忽略行间距,暂时用8
  48. make.centerX.mas_equalTo(0);
  49. make.left.mas_equalTo(20);
  50. make.right.mas_equalTo(-20);
  51. }];
  52. [self.contentView addSubview:self.hLineView];
  53. [self.hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.right.mas_equalTo(0);
  55. make.bottom.mas_equalTo(-44);
  56. make.height.mas_equalTo(1);
  57. }];
  58. [self.contentView addSubview:self.vLineView];
  59. [self.vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
  60. make.centerX.mas_equalTo(0);
  61. make.width.mas_equalTo(1);
  62. make.top.equalTo(self.hLineView.mas_bottom);
  63. make.bottom.mas_equalTo(0);
  64. }];
  65. [self.contentView addSubview:self.cancelButton];
  66. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  67. make.top.equalTo(self.hLineView.mas_bottom);
  68. make.left.bottom.mas_equalTo(0);
  69. make.width.equalTo(self.contentView.mas_width).multipliedBy(0.5);
  70. }];
  71. [self.contentView addSubview:self.confirmButton];
  72. [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.top.equalTo(self.hLineView.mas_bottom);
  74. make.right.bottom.mas_equalTo(0);
  75. make.width.equalTo(self.contentView.mas_width).multipliedBy(0.5);
  76. }];
  77. }
  78. - (void)cancelButtonAction {
  79. if (self.cancelBlock) {
  80. self.cancelBlock();
  81. }
  82. [self dismiss];
  83. }
  84. - (void)confirmButtonAction {
  85. if (self.confirmBlock) {
  86. self.confirmBlock();
  87. }
  88. [self dismiss];
  89. }
  90. - (void)setIsSingleBtn:(BOOL)isSingleBtn {
  91. _isSingleBtn = isSingleBtn;
  92. if (isSingleBtn) {
  93. self.cancelButton.hidden = YES;
  94. self.vLineView.hidden = YES;
  95. [self.confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) {
  96. make.top.equalTo(self.hLineView.mas_bottom);
  97. make.right.bottom.mas_equalTo(0);
  98. make.width.equalTo(self.contentView.mas_width).multipliedBy(1.0);
  99. }];
  100. }
  101. }
  102. - (void)setTitleText:(NSString *)titleText {
  103. _titleText = titleText;
  104. self.titleLabel.text = titleText;
  105. }
  106. - (void)setContentText:(NSString *)contentText {
  107. _contentText = contentText;
  108. self.contentLabel.text = contentText;
  109. }
  110. - (void)setCancelButtonText:(NSString *)cancelButtonText {
  111. _cancelButtonText = cancelButtonText;
  112. [self.cancelButton setTitle:cancelButtonText forState:UIControlStateNormal];
  113. }
  114. - (void)setConfirmButtonText:(NSString *)confirmButtonText {
  115. _confirmButtonText = confirmButtonText;
  116. [self.confirmButton setTitle:confirmButtonText forState:UIControlStateNormal];
  117. }
  118. - (void)calculateHeight{
  119. //标题高度
  120. CGFloat titleHeight = [MOTools calculateRowHeight:self.titleLabel.text font:self.titleLabel.font andWidth:kContentViewWidth - 40];
  121. CGFloat contentHeight = 0;
  122. if (self.contentLabel.text.length > 0) {
  123. contentHeight = [MOTools calculateRowHeight:self.contentLabel.text font:self.contentLabel.font andWidth:kContentViewWidth - 40];;
  124. }
  125. //标题顶部距离
  126. CGFloat titleTopSpacing = 24.0;
  127. //内容占用距离
  128. CGFloat contentLabelHeight = (contentHeight > 0) ? contentHeight + 5 : 0;//5是补偿行间距
  129. //文字内容底部距离
  130. CGFloat textBottomSpacing = 24.0;
  131. //按钮高度
  132. CGFloat buttonHeight = 44.0;
  133. CGFloat contentViewHeight = titleTopSpacing + titleHeight + contentLabelHeight + textBottomSpacing + buttonHeight;
  134. [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
  135. make.height.mas_equalTo(contentViewHeight);
  136. }];
  137. }
  138. - (void)show {
  139. [self calculateHeight];
  140. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  141. [keyWindow addSubview:self];
  142. //动画效果
  143. self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2);
  144. self.contentView.alpha = 0;
  145. [UIView animateWithDuration:0.2 animations:^ {
  146. self.contentView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  147. self.contentView.alpha = 1;
  148. } completion:nil];
  149. }
  150. - (void)dismiss {
  151. [UIView animateWithDuration:0.2 animations:^ {
  152. self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2);
  153. self.contentView.alpha = 0;
  154. } completion:^(BOOL finished) {
  155. if (finished) {
  156. [self.bgView removeFromSuperview];
  157. [self.contentView removeFromSuperview];
  158. [self removeFromSuperview];
  159. }
  160. }];
  161. }
  162. #pragma mark - Lazy
  163. - (UIView *)bgView {
  164. if (!_bgView) {
  165. _bgView = [[UIView alloc] init];
  166. _bgView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.4];
  167. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  168. [_bgView addGestureRecognizer:tap];
  169. }
  170. return _bgView;
  171. }
  172. - (UIView *)contentView {
  173. if (!_contentView) {
  174. _contentView = [[UIView alloc] init];
  175. _contentView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  176. _contentView.layer.cornerRadius = 16;
  177. _contentView.layer.masksToBounds = YES;
  178. }
  179. return _contentView;
  180. }
  181. - (UILabel *)titleLabel {
  182. if (!_titleLabel) {
  183. _titleLabel = [[UILabel alloc] init];
  184. _titleLabel.font = [MOTextTools mediumFont:16];
  185. _titleLabel.textColor = kBaseTextColor_1;
  186. _titleLabel.textAlignment = NSTextAlignmentCenter;
  187. _titleLabel.numberOfLines = 0;
  188. }
  189. return _titleLabel;
  190. }
  191. - (UILabel *)contentLabel {
  192. if (!_contentLabel) {
  193. _contentLabel = [[UILabel alloc] init];
  194. _contentLabel.font = [MOTextTools regularFont:14];
  195. _contentLabel.textColor = [MOTools colorWithHexString:@"#5C5E66"];
  196. _contentLabel.textAlignment = NSTextAlignmentCenter;
  197. _contentLabel.numberOfLines = 0;
  198. }
  199. return _contentLabel;
  200. }
  201. - (UIButton *)cancelButton {
  202. if (!_cancelButton) {
  203. _cancelButton = [[UIButton alloc] init];
  204. _cancelButton.titleLabel.font = [MOTextTools regularFont:16];
  205. [_cancelButton setTitleColor:[MOTools colorWithHexString:@"#5C5E66"] forState:UIControlStateNormal];
  206. [_cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
  207. }
  208. return _cancelButton;
  209. }
  210. - (UIButton *)confirmButton {
  211. if (!_confirmButton) {
  212. _confirmButton = [[UIButton alloc] init];
  213. _confirmButton.titleLabel.font = [MOTextTools mediumFont:16];
  214. [_confirmButton setTitleColor:[MOTools colorWithHexString:@"#4363FF"] forState:UIControlStateNormal];
  215. [_confirmButton addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
  216. }
  217. return _confirmButton;
  218. }
  219. - (UIView *)hLineView {
  220. if (!_hLineView) {
  221. _hLineView = [[UIView alloc] init];
  222. _hLineView.backgroundColor = [MOTools colorWithHexString:@"#DADCE6"];
  223. }
  224. return _hLineView;
  225. }
  226. - (UIView *)vLineView {
  227. if (!_vLineView) {
  228. _vLineView = [[UIView alloc] init];
  229. _vLineView.backgroundColor = [MOTools colorWithHexString:@"#DADCE6"];
  230. }
  231. return _vLineView;
  232. }
  233. @end