// // MORedPacketCustomView.m // MiMoLive // // Created by MiMo on 2025/5/16. // #import "MORedPacketCustomView.h" #import "MORedSendConfigs.h" @interface MORedPacketCustomView () /** 背景 */ @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UIView *containerView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIView *customContainerView; @property (nonatomic, strong) UITextField *textField; @property (nonatomic, strong) BigBtn *minusBtn; @property (nonatomic, strong) BigBtn *plusBtn; @property (nonatomic, strong) BigBtn *cancelBtn; @property (nonatomic, strong) BigBtn *confirmBtn; @end @implementation MORedPacketCustomView - (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.containerView]; [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(keyWindow); make.centerX.equalTo(keyWindow); make.width.mas_equalTo(305); make.height.mas_equalTo(213); }]; //标题 [self.containerView addSubview:self.titleLabel]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.containerView).with.offset(16.0); make.right.equalTo(self.containerView).with.offset(-16.0); make.top.equalTo(self.containerView).with.offset(25); }]; [self.containerView addSubview:self.customContainerView]; [self.customContainerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.titleLabel.mas_bottom).offset(25); make.centerX.mas_equalTo(0); make.width.mas_equalTo(198); make.height.mas_equalTo(49); }]; [self.customContainerView addSubview:self.minusBtn]; [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(0); make.left.mas_equalTo(30); make.size.mas_equalTo(CGSizeMake(10, 10)); }]; [self.customContainerView addSubview:self.textField]; [self.textField mas_makeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(0); make.size.mas_equalTo(CGSizeMake(80, 19)); }]; [self.customContainerView addSubview:self.plusBtn]; [self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(0); make.right.mas_equalTo(-30); make.size.mas_equalTo(CGSizeMake(10, 10)); }]; //取消按钮 [self.containerView addSubview:self.cancelBtn]; [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.equalTo(self.containerView).offset(-25); make.left.equalTo(self.containerView).offset(25); make.width.mas_equalTo(120); make.height.mas_equalTo(44); }]; //确定Button [self.containerView addSubview:self.confirmBtn]; [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.cancelBtn.mas_right).offset(14); make.width.mas_equalTo(122); make.height.mas_equalTo(46); make.centerY.equalTo(self.cancelBtn); }]; } #pragma mark - my method - (void)setType:(CustomRedPacketType)type { _type = type; if (type == CustomRedPacketTypeAmount) { self.titleLabel.text = NSLocalString(@"mimo_red_envelope_diamond_title"); } else { self.textField.rightView = nil; self.titleLabel.text = NSLocalString(@"mimo_red_envelope_quantity_title"); } } - (void)setConfig:(MORedSendConfigs *)config { _config = config; if (self.type == CustomRedPacketTypeAmount) { self.textField.text = [NSString stringWithFormat:@"%.0f", config.minAmount]; if (config.customAmountValue > 0) { self.textField.text = [NSString stringWithFormat:@"%zd", config.customAmountValue]; } } else { self.textField.text = [NSString stringWithFormat:@"%.0f", config.minNum]; if (config.customQuantityValue > 0) { self.textField.text = [NSString stringWithFormat:@"%zd", config.customQuantityValue]; } } } //界面显示动画 - (void)show { UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window; [keyWindow addSubview:self]; //动画效果 self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3); self.containerView.alpha = 0; [UIView animateWithDuration:0.2 animations:^ { self.containerView.transform = CGAffineTransformMakeScale(1.0, 1.0); self.containerView.alpha = 1; } completion:^(BOOL finished) { }]; } //点击确认 - (void)confirmBtnClick { //点击确认 if (self.confirmBlock) { self.confirmBlock([self.textField.text integerValue]); } [self dismiss]; } - (void)cancelBtnClick { if (self.cancelBlock) { self.cancelBlock(); } [self dismiss]; } - (void)minusBtnClick { NSInteger offset = 0; NSInteger limitValue = 0; if (self.type == CustomRedPacketTypeAmount) {//金额 if (self.config.scope == 1) { // 普通红包 offset = 1000; } else {//全服红包 offset = 10000; } limitValue = self.config.minAmount; } else {//数量 offset = 1; limitValue = self.config.minNum; } NSInteger value = [self.textField.text integerValue]; value -= offset; if (value < limitValue) { value = limitValue; }; self.textField.text = [NSString stringWithFormat:@"%zd", value]; } - (void)plusBtnClick { NSInteger offset = 0; NSInteger limitValue = 0; if (self.type == CustomRedPacketTypeAmount) {//金额 if (self.config.scope == 1) { // 普通红包 offset = 1000; } else {//全服红包 offset = 10000; } limitValue = self.config.maxAmount; } else {//数量 offset = 1; limitValue = self.config.maxNum; } NSInteger value = [self.textField.text integerValue]; value += offset; if (value > limitValue) { value = limitValue; }; self.textField.text = [NSString stringWithFormat:@"%zd", value]; } //取消掉键盘 - (void)dismiss { [UIView animateWithDuration:0.2 animations:^ { self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3); self.containerView.alpha = 0; } completion:^(BOOL finished) { if (finished) { [self.bgView removeFromSuperview]; [self.containerView removeFromSuperview]; [self removeFromSuperview]; } }]; } - (void)textFieldValueChanged:(UITextField *)textField { NSInteger minNum = 0; NSInteger maxNum = 0; if (self.type == CustomRedPacketTypeAmount) {//金额 minNum = self.config.minAmount; maxNum = self.config.maxAmount; } else {//数量 minNum = self.config.minNum; maxNum = self.config.maxNum; } NSInteger value = [textField.text integerValue]; BOOL valid = (value >= minNum && value <= maxNum); self.confirmBtn.enabled = valid; self.confirmBtn.alpha = valid ? 1.0 : 0.8; } #pragma mark - 懒加载 - (UIView *)bgView { if (!_bgView) { _bgView = [[UIView alloc] init]; _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTapClick)]; [_bgView addGestureRecognizer:tap]; } return _bgView; } - (void)bgViewTapClick { [self dismiss]; } - (UIView *)containerView { if (!_containerView) { _containerView = [[UIView alloc] init]; _containerView.backgroundColor = [MOTools colorWithHexString:@"#FFF5F1" alpha:1.0]; _containerView.layer.masksToBounds = YES; _containerView.layer.cornerRadius = 20.0; } return _containerView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = [MOTools colorWithHexString:@"#8A3900" alpha:1.0];; _titleLabel.font = [MOTextTools getTheFontWithSize:17.0 AndFontName:kNormalContentBlodFontStr]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.numberOfLines = 0; _titleLabel.text = @""; } return _titleLabel; } - (UIView *)customContainerView { if (!_customContainerView) { _customContainerView = [[UIView alloc] init]; _customContainerView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0]; _customContainerView.layer.masksToBounds = YES; _customContainerView.layer.cornerRadius = 13.0; } return _customContainerView; } - (BigBtn *)minusBtn { if (!_minusBtn) { _minusBtn = [[BigBtn alloc] init]; [_minusBtn addTarget:self action:@selector(minusBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_minusBtn setImage:[UIImage imageNamed:@"icon_red_packet_minus"] forState:UIControlStateNormal]; } return _minusBtn; } - (UITextField *)textField { if (!_textField) { _textField = [[UITextField alloc] init]; _textField.textAlignment = NSTextAlignmentCenter; _textField.textColor = [MOTools colorWithHexString:@"#8A3900"]; _textField.font = [MOTextTools getTheFontWithSize:16.0 AndFontName:kNormalContentBlodFontStr]; _textField.keyboardType = UIKeyboardTypeNumberPad; [self.textField addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged]; UIImageView *diamondIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_red_list_diamond"]]; diamondIconView.frame = CGRectMake(0, 0, 19, 14); _textField.rightView = diamondIconView; _textField.rightViewMode = UITextFieldViewModeAlways; } return _textField; } - (BigBtn *)plusBtn { if (!_plusBtn) { _plusBtn = [[BigBtn alloc] init]; [_plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_plusBtn setImage:[UIImage imageNamed:@"icon_red_packet_plus"] forState:UIControlStateNormal]; } return _plusBtn; } - (BigBtn *)cancelBtn { if (!_cancelBtn) { _cancelBtn = [[BigBtn alloc] init]; _cancelBtn.titleLabel.font = [MOTextTools getTheFontWithSize:15.0 AndFontName:kNormalContentFontStr]; [_cancelBtn setBackgroundColor:[MOTools colorWithHexString:@"#FFFFFF" alpha:1.0]]; _cancelBtn.layer.masksToBounds = YES; _cancelBtn.layer.cornerRadius = 22; [_cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_cancelBtn setTitleColor:[MOTools colorWithHexString:@"#666666"] forState:UIControlStateNormal]; } return _cancelBtn; } - (BigBtn *)confirmBtn { if (!_confirmBtn) { _confirmBtn = [[BigBtn alloc] init]; _confirmBtn.titleLabel.font = [MOTextTools getTheFontWithSize:15.0 AndFontName:kNormalContentBlodFontStr]; [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside]; [_confirmBtn setTitle:@"Save" forState:UIControlStateNormal]; [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_send_red_packet_btn"] forState:UIControlStateNormal]; } return _confirmBtn; } @end