// // MOSystemAlertView.m // MiMoLive // // Created by MiMo on 2025/9/17. // #define kContentViewWidth kScaleWidth(295) #import "MOSystemAlertView.h" @interface MOSystemAlertView() @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UIView *contentView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *contentLabel; @property (nonatomic, strong) UIView *hLineView;//横线 @property (nonatomic, strong) UIView *vLineView;//竖线 @property (nonatomic, strong) UIButton *cancelButton; @property (nonatomic, strong) UIButton *confirmButton; @end @implementation MOSystemAlertView - (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.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(24); make.centerX.mas_equalTo(0); make.left.mas_equalTo(20); make.right.mas_equalTo(-20); }]; [self.contentView addSubview:self.contentLabel]; [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.titleLabel.mas_bottom).offset(8);//忽略行间距,暂时用8 make.centerX.mas_equalTo(0); make.left.mas_equalTo(20); make.right.mas_equalTo(-20); }]; [self.contentView addSubview:self.hLineView]; [self.hLineView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(0); make.bottom.mas_equalTo(-44); make.height.mas_equalTo(1); }]; [self.contentView addSubview:self.vLineView]; [self.vLineView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(0); make.width.mas_equalTo(1); make.top.equalTo(self.hLineView.mas_bottom); make.bottom.mas_equalTo(0); }]; [self.contentView addSubview:self.cancelButton]; [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.hLineView.mas_bottom); make.left.bottom.mas_equalTo(0); make.width.equalTo(self.contentView.mas_width).multipliedBy(0.5); }]; [self.contentView addSubview:self.confirmButton]; [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.hLineView.mas_bottom); make.right.bottom.mas_equalTo(0); make.width.equalTo(self.contentView.mas_width).multipliedBy(0.5); }]; } - (void)cancelButtonAction { if (self.cancelBlock) { self.cancelBlock(); } [self dismiss]; } - (void)confirmButtonAction { if (self.confirmBlock) { self.confirmBlock(); } [self dismiss]; } - (void)setIsSingleBtn:(BOOL)isSingleBtn { _isSingleBtn = isSingleBtn; if (isSingleBtn) { self.cancelButton.hidden = YES; self.vLineView.hidden = YES; [self.confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.hLineView.mas_bottom); make.right.bottom.mas_equalTo(0); make.width.equalTo(self.contentView.mas_width).multipliedBy(1.0); }]; } } - (void)setTitleText:(NSString *)titleText { _titleText = titleText; self.titleLabel.text = titleText; } - (void)setContentText:(NSString *)contentText { _contentText = contentText; self.contentLabel.text = contentText; } - (void)setCancelButtonText:(NSString *)cancelButtonText { _cancelButtonText = cancelButtonText; [self.cancelButton setTitle:cancelButtonText forState:UIControlStateNormal]; } - (void)setConfirmButtonText:(NSString *)confirmButtonText { _confirmButtonText = confirmButtonText; [self.confirmButton setTitle:confirmButtonText forState:UIControlStateNormal]; } - (void)calculateHeight{ //标题高度 CGFloat titleHeight = [MOTools calculateRowHeight:self.titleLabel.text font:self.titleLabel.font andWidth:kContentViewWidth - 40]; CGFloat contentHeight = 0; if (self.contentLabel.text.length > 0) { contentHeight = [MOTools calculateRowHeight:self.contentLabel.text font:self.contentLabel.font andWidth:kContentViewWidth - 40];; } //标题顶部距离 CGFloat titleTopSpacing = 24.0; //内容占用距离 CGFloat contentLabelHeight = (contentHeight > 0) ? contentHeight + 5 : 0;//5是补偿行间距 //文字内容底部距离 CGFloat textBottomSpacing = 24.0; //按钮高度 CGFloat buttonHeight = 44.0; CGFloat contentViewHeight = titleTopSpacing + titleHeight + contentLabelHeight + textBottomSpacing + buttonHeight; [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 *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.font = [MOTextTools mediumFont:16]; _titleLabel.textColor = kBaseTextColor_1; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.numberOfLines = 0; } return _titleLabel; } - (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"] forState:UIControlStateNormal]; [_cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _cancelButton; } - (UIButton *)confirmButton { if (!_confirmButton) { _confirmButton = [[UIButton alloc] init]; _confirmButton.titleLabel.font = [MOTextTools mediumFont:16]; [_confirmButton setTitleColor:[MOTools colorWithHexString:@"#4363FF"] forState:UIControlStateNormal]; [_confirmButton addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _confirmButton; } - (UIView *)hLineView { if (!_hLineView) { _hLineView = [[UIView alloc] init]; _hLineView.backgroundColor = [MOTools colorWithHexString:@"#DADCE6"]; } return _hLineView; } - (UIView *)vLineView { if (!_vLineView) { _vLineView = [[UIView alloc] init]; _vLineView.backgroundColor = [MOTools colorWithHexString:@"#DADCE6"]; } return _vLineView; } @end