// // SPSBConfirmAlertViewController.m // 我的社保 // // Created by shanp on 2021/4/26. // #import "SPSBConfirmAlertViewController.h" #import "SPSBUIGeneralHeader.h" @interface SPSBConfirmAlertViewController () { NSString *_title; NSString *_content; UIView *_alertView; UIButton *_cancelButton; UIButton *_confirmButton; void(^_Nullable _cancelAction)(void); void(^_Nullable _confirmAction)(void); } @end @implementation SPSBConfirmAlertViewController + (instancetype)alertWithTitle:(nullable NSString *)title content:(nullable NSString *)content { SPSBConfirmAlertViewController *alert = SPSBConfirmAlertViewController.new; alert->_title = title; alert->_content = content; return alert; } - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; } #pragma mark - Action - (void)confirmAction { [self closeActionCompletion:^{ if (self->_confirmAction) { self->_confirmAction(); } }]; } - (void)cancelAction { [self closeActionCompletion:^{ if (self->_cancelAction) { self->_cancelAction(); } }]; } - (void)closeActionCompletion: (void (^_Nullable)(void))completion { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; animation.fromValue = [NSNumber numberWithFloat:1.0]; animation.toValue = [NSNumber numberWithFloat:0.8]; animation.duration = 0.25; animation.removedOnCompletion = true; [_alertView.layer addAnimation:animation forKey:nil]; [UIView animateWithDuration:0.25 animations:^{ self->_alertView.alpha = 0.f; self.view.backgroundColor =spsb_000000_color(0.f); } completion:^(BOOL finished) { [self dismissViewControllerAnimated:false completion:completion]; }]; } - (void)showAnimation { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; animation.fromValue = [NSNumber numberWithFloat:1.2]; animation.toValue = [NSNumber numberWithFloat:1.0]; animation.duration = 0.25; animation.removedOnCompletion = true; [_alertView.layer addAnimation:animation forKey:nil]; [UIView animateWithDuration:0.25 animations:^{ self.view.backgroundColor = spsb_000000_color(SPSBPopupViewBackgroundAlpha); self->_alertView.alpha = 1.f; }]; } - (void)setupAnimation { self.view.backgroundColor = spsb_000000_color(0.f); } #pragma mark - UI - (void)setupUI { [self createAlert]; } #define byh_widthLimit 253 - (void)setCancelButtonTitle:(nullable NSString *)title titleColor:(nullable UIColor *)color action:(void(^_Nullable)(void))action { _cancelButton = [UIButton convenienceWithFont:spsb_font(18) target:self action:@selector(cancelAction)]; [_cancelButton setTitle:title ?: @"取消" titleColor:color ?: spsb_3296FB_color() image:nil backgroundImage:jxh_createImage(spsb_FFFFFF_color(1.f), (CGSize){50, 50}) state:JXHButtonControlStateNormal]; [_cancelButton setBackgroundImage:jxh_createImage(spsb_000000_color(0.1), (CGSize){50, 50}) state:JXHButtonControlStateHighlighted]; _cancelAction = action; } - (void)setConfirmButtonTitle:(nullable NSString *)title titleColor:(nullable UIColor *)color action:(void(^_Nullable)(void))action { _confirmButton = [UIButton convenienceWithFont:spsb_mediumFont(18) target:self action:@selector(confirmAction)]; [_confirmButton setTitle:title ?: @"确定" titleColor:color ?: spsb_3296FB_color() image:nil backgroundImage:jxh_createImage(spsb_FFFFFF_color(1.f), (CGSize){50, 50}) state:JXHButtonControlStateNormal]; [_confirmButton setBackgroundImage:jxh_createImage(spsb_000000_color(0.1), (CGSize){50, 50}) state:JXHButtonControlStateHighlighted]; _confirmAction = action; } - (void)createAlert { _alertView = UIView.new; _alertView.backgroundColor = spsb_FFFFFF_color(1.f); _alertView.alpha = 0.f; [_alertView setLayerCornerRadius:14 clipToBounds:true]; [self.view addSubview:_alertView]; [_alertView makeConstraints:^(JXHConstraintMaker *make) { make.center.equalTo(self.view); make.width.equalTo(313); }]; UIView *contentView = UIView.new; [_alertView addSubview:contentView]; [contentView makeConstraints:^(JXHConstraintMaker *make) { make.top.and.leading.and.trailing.equalTo(0); make.height.greaterThanOrEqualTo(60).priorityRequired(); }]; NSString *t = _title ?: @""; UILabel *title = [UILabel convenienceWithFont:spsb_mediumFont(18) textColor:spsb_333333_color() limitWidth:byh_widthLimit]; [title setAttributedStringWithText:t textAlignment:NSTextAlignmentCenter lineSpacing:3 lineHeight:0 paragraphSpacing:0 lineBreakMode:0 lines:jxh_getLinesNum(t, byh_widthLimit, spsb_mediumFont(18))]; [title setWordKern:-0.53]; [contentView addSubview:title]; NSString *c = _content ?: @""; UILabel *content = [UILabel convenienceWithFont:spsb_font(16) textColor:spsb_666666_color() limitWidth:byh_widthLimit]; [content setAttributedStringWithText:c textAlignment:NSTextAlignmentCenter lineSpacing:5 lineHeight:0 paragraphSpacing:0 lineBreakMode:0 lines:jxh_getLinesNum(c, byh_widthLimit, spsb_font(16))]; [contentView addSubview:content]; if (_title && _content) { [title makeConstraints:^(JXHConstraintMaker *make) { make.centerX.equalTo(contentView); make.top.equalTo(28); make.height.lessThanOrEqualTo(jxh_screenHeight() / 3).priorityRequired(); }]; [content makeConstraints:^(JXHConstraintMaker *make) { make.centerX.equalTo(contentView); make.top.equalTo(title.bottom).offset(8); make.bottom.equalTo(-28); make.height.lessThanOrEqualTo(jxh_screenHeight() / 3).priorityRequired(); }]; } else if (_title) { [title makeConstraints:^(JXHConstraintMaker *make) { make.centerX.equalTo(contentView); make.centerY.equalTo(contentView).priorityRequired(); make.top.greaterThanOrEqualTo(28); make.bottom.lessThanOrEqualTo(-28); make.height.lessThanOrEqualTo(jxh_screenHeight() / 2).priorityRequired(); }]; } else if (_content) { [content makeConstraints:^(JXHConstraintMaker *make) { make.centerX.equalTo(contentView); make.centerY.equalTo(contentView).priorityRequired(); make.top.greaterThanOrEqualTo(28); make.bottom.lessThanOrEqualTo(-28); make.height.lessThanOrEqualTo(jxh_screenHeight() / 2).priorityRequired(); }]; } [contentView createLineWithLocation:JXHLineLocationBottom]; UIView *buttonView = UIView.new; [_alertView addSubview:buttonView]; [buttonView makeConstraints:^(JXHConstraintMaker *make) { make.bottom.and.leading.and.trailing.equalTo(0); make.top.equalTo(contentView.bottom); make.height.equalTo(50); }]; if (!_confirmButton && !_cancelButton) { [self setConfirmButtonTitle:nil titleColor:nil action:nil]; } if (_confirmButton && _cancelButton) { [buttonView addSubview:_cancelButton]; [_cancelButton makeConstraints:^(JXHConstraintMaker *make) { make.top.and.bottom.and.leading.equalTo(0); }]; [buttonView addSubview:_confirmButton]; [_confirmButton makeConstraints:^(JXHConstraintMaker *make) { make.top.and.bottom.and.trailing.equalTo(0); make.leading.equalTo(self->_cancelButton.trailing); make.width.equalTo(self->_cancelButton); }]; [buttonView createLineWithLocation:JXHLineLocationHorizontalCenter]; } else if (_confirmButton) { [buttonView addSubview:_confirmButton]; [_confirmButton makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(buttonView); }]; } else { [buttonView addSubview:_cancelButton]; [_cancelButton makeConstraints:^(JXHConstraintMaker *make) { make.edges.equalTo(buttonView); }]; } } @end