// // MOThreeOptionAlertView.m // MiMoLive // // Created by SuperC on 2025/9/18. // #define kContentViewWidth kScaleWidth(295) #import "MOThreeOptionAlertView.h" @interface MOThreeOptionAlertView () @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UILabel *contentLabel; @property (nonatomic, strong) UIButton *cancelButton; @property (nonatomic, strong) UIButton *confirmOneButton; @property (nonatomic, strong) UIButton *confirmTwoButton; @end @implementation MOThreeOptionAlertView - (instancetype)init { if (self = [super init]) { [self setupUI]; } return self; } - (void)setupUI{ UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window; [keyWindow addSubview:self.bgView]; [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.bottom.right.equalTo(keyWindow); }]; [keyWindow addSubview:self.contentView]; [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(kContentViewWidth); make.center.mas_equalTo(0); make.height.mas_equalTo(116); }]; [self.contentView addSubview:self.contentLabel]; [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(20); make.left.mas_equalTo(20); make.right.mas_equalTo(-20); }]; [self.contentView addSubview:self.cancelButton]; [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.left.right.equalTo(self.contentView); make.height.equalTo(@44.0); }]; UIView *lineViewOne = [[UIView alloc] init]; lineViewOne.backgroundColor = [MOTools colorWithHexString:@"#DADCE6" alpha:1.0]; [self.contentView addSubview:lineViewOne]; [lineViewOne mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.cancelButton.mas_top); make.left.right.equalTo(self.contentView); make.height.equalTo(@0.5); }]; [self.contentView addSubview:self.confirmTwoButton]; [self.confirmTwoButton mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(lineViewOne.mas_top); make.left.right.equalTo(self.contentView); make.height.equalTo(@44.0); }]; UIView *lineViewTwo = [[UIView alloc] init]; lineViewTwo.backgroundColor = [MOTools colorWithHexString:@"#DADCE6" alpha:1.0]; [self.contentView addSubview:lineViewTwo]; [lineViewTwo mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.confirmTwoButton.mas_top); make.left.right.equalTo(self.contentView); make.height.equalTo(@0.5); }]; [self.contentView addSubview:self.confirmOneButton]; [self.confirmOneButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.contentView); make.height.equalTo(@44.0); make.bottom.equalTo(lineViewTwo.mas_top); }]; UIView *lineViewThree = [[UIView alloc] init]; lineViewThree.backgroundColor = [MOTools colorWithHexString:@"#DADCE6" alpha:1.0]; [self.contentView addSubview:lineViewThree]; [lineViewThree mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.confirmOneButton.mas_top); make.left.right.equalTo(self.contentView); make.height.equalTo(@0.5); }]; } - (void)setCancelButtonText:(NSString *)cancelButtonText{ _cancelButtonText = cancelButtonText; [self.cancelButton setTitle:cancelButtonText forState:UIControlStateNormal]; } - (void)setConfirmOneButtonText:(NSString *)confirmOneButtonText{ _confirmOneButtonText = confirmOneButtonText; [self.confirmOneButton setTitle:confirmOneButtonText forState:UIControlStateNormal]; } - (void)setConfirmTwoButtonText:(NSString *)confirmTwoButtonText{ _confirmTwoButtonText = confirmTwoButtonText; [self.confirmTwoButton setTitle:confirmTwoButtonText forState:UIControlStateNormal]; } - (void)setContentText:(NSAttributedString *)contentText{ _contentText = contentText; self.contentLabel.attributedText = contentText; } - (void)calculateHeight{ CGFloat contentHeight = 0; if (self.contentLabel.attributedText.length > 0) { NSString *contentStr = [self.contentLabel.attributedText string]; contentHeight = [MOTools calculateRowHeight:contentStr font:self.contentLabel.font andWidth:kContentViewWidth - 40];; } //标题顶部距离 CGFloat titleTopSpacing = 24.0; //文字内容底部距离 CGFloat textBottomSpacing = 24.0; //按钮高度 CGFloat buttonHeight = 44.0; CGFloat contentViewHeight = titleTopSpacing + contentHeight + textBottomSpacing + buttonHeight * 3.0; [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(contentViewHeight); }]; } - (void)show { [self calculateHeight]; UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window; [keyWindow addSubview:self]; //动画效果 self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2); self.contentView.alpha = 0; [UIView animateWithDuration:0.2 animations:^ { self.contentView.transform = CGAffineTransformMakeScale(1.0, 1.0); self.contentView.alpha = 1; } completion:nil]; } - (void)dismiss { [UIView animateWithDuration:0.2 animations:^ { self.contentView.transform = CGAffineTransformMakeScale(1.2, 1.2); self.contentView.alpha = 0; } completion:^(BOOL finished) { if (finished) { [self.bgView removeFromSuperview]; [self.contentView removeFromSuperview]; [self removeFromSuperview]; } }]; } #pragma mark - Lazy - (UIView *)bgView { if (!_bgView) { _bgView = [[UIView alloc] init]; _bgView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.4]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)]; [_bgView addGestureRecognizer:tap]; } return _bgView; } - (UIView *)contentView { if (!_contentView) { _contentView = [[UIView alloc] init]; _contentView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"]; _contentView.layer.cornerRadius = 16; _contentView.layer.masksToBounds = YES; } return _contentView; } - (UILabel *)contentLabel { if (!_contentLabel) { _contentLabel = [[UILabel alloc] init]; _contentLabel.font = [MOTextTools regularFont:14]; _contentLabel.textColor = [MOTools colorWithHexString:@"#5C5E66"]; _contentLabel.textAlignment = NSTextAlignmentCenter; _contentLabel.numberOfLines = 0; } return _contentLabel; } - (UIButton *)cancelButton { if (!_cancelButton) { _cancelButton = [[UIButton alloc] init]; _cancelButton.titleLabel.font = [MOTextTools regularFont:16]; [_cancelButton setTitleColor:[MOTools colorWithHexString:@"#5C5E66" alpha:0.5] forState:UIControlStateNormal]; [_cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } - (void)cancelButtonAction{ [self dismiss]; } - (UIButton *)confirmOneButton { if (!_confirmOneButton) { _confirmOneButton = [[UIButton alloc] init]; _confirmOneButton.titleLabel.font = [MOTextTools mediumFont:16]; [_confirmOneButton setTitleColor:[MOTools colorWithHexString:@"#4363FF"] forState:UIControlStateNormal]; [_confirmOneButton addTarget:self action:@selector(confirmOneButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _confirmOneButton; } - (void)confirmOneButtonAction{ self.confirmOneBlock ? self.confirmOneBlock() : nil; [self dismiss]; } - (UIButton *)confirmTwoButton { if (!_confirmTwoButton) { _confirmTwoButton = [[UIButton alloc] init]; _confirmTwoButton.titleLabel.font = [MOTextTools mediumFont:16]; [_confirmTwoButton setTitleColor:kBaseTextColor_2 forState:UIControlStateNormal]; [_confirmTwoButton addTarget:self action:@selector(confirmTwoButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _confirmTwoButton; } - (void)confirmTwoButtonAction{ self.confirmTwoBlock ? self.confirmTwoBlock() : nil; [self dismiss]; } @end