MOThreeOptionAlertView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // MOThreeOptionAlertView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2025/9/18.
  6. //
  7. #define kContentViewWidth kScaleWidth(295)
  8. #import "MOThreeOptionAlertView.h"
  9. @interface MOThreeOptionAlertView ()
  10. @property (nonatomic, strong) UIView *bgView;
  11. @property (nonatomic, strong) UIView *contentView;
  12. @property (nonatomic, strong) UILabel *contentLabel;
  13. @property (nonatomic, strong) UIButton *cancelButton;
  14. @property (nonatomic, strong) UIButton *confirmOneButton;
  15. @property (nonatomic, strong) UIButton *confirmTwoButton;
  16. @end
  17. @implementation MOThreeOptionAlertView
  18. - (instancetype)init {
  19. if (self = [super init]) {
  20. [self setupUI];
  21. }
  22. return self;
  23. }
  24. - (void)setupUI{
  25. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  26. [keyWindow addSubview:self.bgView];
  27. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  28. make.top.left.bottom.right.equalTo(keyWindow);
  29. }];
  30. [keyWindow addSubview:self.contentView];
  31. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.width.mas_equalTo(kContentViewWidth);
  33. make.center.mas_equalTo(0);
  34. make.height.mas_equalTo(116);
  35. }];
  36. [self.contentView addSubview:self.contentLabel];
  37. [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  38. make.top.mas_equalTo(20);
  39. make.left.mas_equalTo(20);
  40. make.right.mas_equalTo(-20);
  41. }];
  42. [self.contentView addSubview:self.cancelButton];
  43. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.bottom.left.right.equalTo(self.contentView);
  45. make.height.equalTo(@44.0);
  46. }];
  47. UIView *lineViewOne = [[UIView alloc] init];
  48. lineViewOne.backgroundColor = [MOTools colorWithHexString:@"#DADCE6" alpha:1.0];
  49. [self.contentView addSubview:lineViewOne];
  50. [lineViewOne mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.bottom.equalTo(self.cancelButton.mas_top);
  52. make.left.right.equalTo(self.contentView);
  53. make.height.equalTo(@0.5);
  54. }];
  55. [self.contentView addSubview:self.confirmTwoButton];
  56. [self.confirmTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.bottom.equalTo(lineViewOne.mas_top);
  58. make.left.right.equalTo(self.contentView);
  59. make.height.equalTo(@44.0);
  60. }];
  61. UIView *lineViewTwo = [[UIView alloc] init];
  62. lineViewTwo.backgroundColor = [MOTools colorWithHexString:@"#DADCE6" alpha:1.0];
  63. [self.contentView addSubview:lineViewTwo];
  64. [lineViewTwo mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.bottom.equalTo(self.confirmTwoButton.mas_top);
  66. make.left.right.equalTo(self.contentView);
  67. make.height.equalTo(@0.5);
  68. }];
  69. [self.contentView addSubview:self.confirmOneButton];
  70. [self.confirmOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.left.right.equalTo(self.contentView);
  72. make.height.equalTo(@44.0);
  73. make.bottom.equalTo(lineViewTwo.mas_top);
  74. }];
  75. UIView *lineViewThree = [[UIView alloc] init];
  76. lineViewThree.backgroundColor = [MOTools colorWithHexString:@"#DADCE6" alpha:1.0];
  77. [self.contentView addSubview:lineViewThree];
  78. [lineViewThree mas_makeConstraints:^(MASConstraintMaker *make) {
  79. make.bottom.equalTo(self.confirmOneButton.mas_top);
  80. make.left.right.equalTo(self.contentView);
  81. make.height.equalTo(@0.5);
  82. }];
  83. }
  84. - (void)setCancelButtonText:(NSString *)cancelButtonText{
  85. _cancelButtonText = cancelButtonText;
  86. [self.cancelButton setTitle:cancelButtonText forState:UIControlStateNormal];
  87. }
  88. - (void)setConfirmOneButtonText:(NSString *)confirmOneButtonText{
  89. _confirmOneButtonText = confirmOneButtonText;
  90. [self.confirmOneButton setTitle:confirmOneButtonText forState:UIControlStateNormal];
  91. }
  92. - (void)setConfirmTwoButtonText:(NSString *)confirmTwoButtonText{
  93. _confirmTwoButtonText = confirmTwoButtonText;
  94. [self.confirmTwoButton setTitle:confirmTwoButtonText forState:UIControlStateNormal];
  95. }
  96. - (void)setContentText:(NSAttributedString *)contentText{
  97. _contentText = contentText;
  98. self.contentLabel.attributedText = contentText;
  99. }
  100. - (void)calculateHeight{
  101. CGFloat contentHeight = 0;
  102. if (self.contentLabel.attributedText.length > 0) {
  103. NSString *contentStr = [self.contentLabel.attributedText string];
  104. contentHeight = [MOTools calculateRowHeight:contentStr font:self.contentLabel.font andWidth:kContentViewWidth - 40];;
  105. }
  106. //标题顶部距离
  107. CGFloat titleTopSpacing = 24.0;
  108. //文字内容底部距离
  109. CGFloat textBottomSpacing = 24.0;
  110. //按钮高度
  111. CGFloat buttonHeight = 44.0;
  112. CGFloat contentViewHeight = titleTopSpacing + contentHeight + textBottomSpacing + buttonHeight * 3.0;
  113. [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
  114. make.height.mas_equalTo(contentViewHeight);
  115. }];
  116. }
  117. - (void)show {
  118. [self calculateHeight];
  119. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  120. [keyWindow addSubview:self];
  121. //动画效果
  122. self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2);
  123. self.contentView.alpha = 0;
  124. [UIView animateWithDuration:0.2 animations:^ {
  125. self.contentView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  126. self.contentView.alpha = 1;
  127. } completion:nil];
  128. }
  129. - (void)dismiss {
  130. [UIView animateWithDuration:0.2 animations:^ {
  131. self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2);
  132. self.contentView.alpha = 0;
  133. } completion:^(BOOL finished) {
  134. if (finished) {
  135. [self.bgView removeFromSuperview];
  136. [self.contentView removeFromSuperview];
  137. [self removeFromSuperview];
  138. }
  139. }];
  140. }
  141. #pragma mark - Lazy
  142. - (UIView *)bgView {
  143. if (!_bgView) {
  144. _bgView = [[UIView alloc] init];
  145. _bgView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.4];
  146. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  147. [_bgView addGestureRecognizer:tap];
  148. }
  149. return _bgView;
  150. }
  151. - (UIView *)contentView {
  152. if (!_contentView) {
  153. _contentView = [[UIView alloc] init];
  154. _contentView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  155. _contentView.layer.cornerRadius = 16;
  156. _contentView.layer.masksToBounds = YES;
  157. }
  158. return _contentView;
  159. }
  160. - (UILabel *)contentLabel {
  161. if (!_contentLabel) {
  162. _contentLabel = [[UILabel alloc] init];
  163. _contentLabel.font = [MOTextTools regularFont:14];
  164. _contentLabel.textColor = [MOTools colorWithHexString:@"#5C5E66"];
  165. _contentLabel.textAlignment = NSTextAlignmentCenter;
  166. _contentLabel.numberOfLines = 0;
  167. }
  168. return _contentLabel;
  169. }
  170. - (UIButton *)cancelButton {
  171. if (!_cancelButton) {
  172. _cancelButton = [[UIButton alloc] init];
  173. _cancelButton.titleLabel.font = [MOTextTools regularFont:16];
  174. [_cancelButton setTitleColor:[MOTools colorWithHexString:@"#5C5E66" alpha:0.5] forState:UIControlStateNormal];
  175. [_cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
  176. }
  177. return _cancelButton;
  178. }
  179. - (void)cancelButtonAction{
  180. [self dismiss];
  181. }
  182. - (UIButton *)confirmOneButton {
  183. if (!_confirmOneButton) {
  184. _confirmOneButton = [[UIButton alloc] init];
  185. _confirmOneButton.titleLabel.font = [MOTextTools mediumFont:16];
  186. [_confirmOneButton setTitleColor:[MOTools colorWithHexString:@"#4363FF"] forState:UIControlStateNormal];
  187. [_confirmOneButton addTarget:self action:@selector(confirmOneButtonAction) forControlEvents:UIControlEventTouchUpInside];
  188. }
  189. return _confirmOneButton;
  190. }
  191. - (void)confirmOneButtonAction{
  192. self.confirmOneBlock ? self.confirmOneBlock() : nil;
  193. [self dismiss];
  194. }
  195. - (UIButton *)confirmTwoButton {
  196. if (!_confirmTwoButton) {
  197. _confirmTwoButton = [[UIButton alloc] init];
  198. _confirmTwoButton.titleLabel.font = [MOTextTools mediumFont:16];
  199. [_confirmTwoButton setTitleColor:kBaseTextColor_2 forState:UIControlStateNormal];
  200. [_confirmTwoButton addTarget:self action:@selector(confirmTwoButtonAction) forControlEvents:UIControlEventTouchUpInside];
  201. }
  202. return _confirmTwoButton;
  203. }
  204. - (void)confirmTwoButtonAction{
  205. self.confirmTwoBlock ? self.confirmTwoBlock() : nil;
  206. [self dismiss];
  207. }
  208. @end