Files
yinmeng-ios/xplan-ios/Main/ModuleKit/SendGiftView/View/XPGiftBarView.m
2022-10-09 16:36:26 +08:00

483 lines
17 KiB
Objective-C

//
// XPGiftBottomView.m
// xplan-ios
//
// Created by 冯硕 on 2021/11/9.
//
#import "XPGiftBarView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "XPMacro.h"
#import "ThemeColor+SendGift.h"
#import "UIImage+Utils.h"
///Model
#import "XPGiftCountModel.h"
#import "WalletInfoModel.h"
///View
#import "XPGiftCountCollectionViewCell.h"
#import "XPGiftCountView.h"
@interface XPGiftBarView ()<UITextFieldDelegate, XPGiftCountViewDelegate>
///余额
@property (nonatomic,strong) UILabel *balanceLabel;
///
@property (nonatomic,strong) UIStackView *rechargeStackView;
///充值
@property (nonatomic,strong) UILabel *rechargeLabel;
///去充值
@property (nonatomic,strong) UIImageView *rechageImageView;
//赠送
@property (nonatomic,strong) UIStackView *sendOperationView;
///个数
@property (nonatomic,strong) UILabel *countLabel;
///箭头
@property (nonatomic,strong) UIButton *arrowButton;
///赠送
@property (nonatomic,strong) UIButton *sendGiftButton;
///首充
@property (nonatomic,strong) UIButton *firstRechargeButton;
///自定义个数
@property (nonatomic,strong) UIView *customCountView;
//输入框
@property (nonatomic, strong) UITextField *editTextFiled;
//发送按钮
@property (nonatomic, strong) UIButton *sureButton;
///送礼物的个数
@property (nonatomic,strong) XPGiftCountView *giftCountView;
@end
@implementation XPGiftBarView
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
[self addNotificationCenter];
[self initEvents];
}
return self;
}
#pragma mark - Response
- (void)sendButtonAction:(UIButton *)sender {
if (self.type == GiftSegmentType_Graffiti && self.drawGiftCount < 10) {
return;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftBarView:didClickSendGift:)]) {
[self.delegate xPGiftBarView:self didClickSendGift:self.giftCountModel];
}
}
- (void)didTagRecharge:(UITapGestureRecognizer *)tap {
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftBarViewDidClickRecharge:)]) {
[self.delegate xPGiftBarViewDidClickRecharge:self];
}
}
- (void)arrowButtonAction:(UIButton *)sender {
if (![[self.superview.superview subviews] containsObject:self.giftCountView]) {
[self.superview.superview addSubview:self.giftCountView];
[self.giftCountView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-15);
make.bottom.mas_equalTo(self.superview.superview.mas_bottom).offset(-45 -kSafeAreaBottomHeight);
make.width.mas_equalTo(135);
}];
}
[self updateCountStatus];
}
- (void)sureButtonAction:(UIButton *)sender {
NSString *countStr = [self.editTextFiled.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (countStr.integerValue == 0) {
countStr = @"1";
}
[self.editTextFiled resignFirstResponder];
self.editTextFiled.text = @"";
self.giftCountModel.giftNumber = countStr;
self.countLabel.text = countStr;
}
- (void)firstRechargeButtonAction:(UIButton *)sender{
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftBarViewDidClickFirstRecharge:)]) {
[self.delegate xPGiftBarViewDidClickFirstRecharge:self];
}
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor= [UIColor clearColor];
[self addSubview:self.balanceLabel];
[self addSubview:self.rechargeStackView];
[self addSubview:self.firstRechargeButton];
[self addSubview:self.sendOperationView];
[self.rechargeStackView addArrangedSubview:self.rechargeLabel];
[self.rechargeStackView addArrangedSubview:self.rechageImageView];
[self.sendOperationView addArrangedSubview:self.countLabel];
[self.sendOperationView addArrangedSubview:self.arrowButton];
[self.sendOperationView addArrangedSubview:self.sendGiftButton];
[self.customCountView addSubview:self.editTextFiled];
[self.customCountView addSubview:self.sureButton];
}
- (void)initSubViewConstraints {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(45);
}];
[self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(15);
make.centerY.mas_equalTo(self.sendOperationView);
}];
[self.rechargeStackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.sendOperationView);
make.left.mas_equalTo(self.balanceLabel.mas_right).offset(12);
}];
[self.firstRechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(50, 14));
make.left.mas_equalTo(self.rechargeStackView.mas_right).offset(4);
make.centerY.mas_equalTo(self.rechargeStackView);
}];
[self.sendOperationView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self).offset(-15);
make.bottom.mas_equalTo(-11);
make.height.mas_equalTo(30);
}];
[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(40);
make.height.mas_equalTo(30);
}];
[self.arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(30, 30));
}];
[self.sendGiftButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(67, 30));
}];
[self.editTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.customCountView).offset(15);
make.right.mas_equalTo(self.sureButton.mas_left).offset(-10);
make.top.bottom.mas_equalTo(self.customCountView);
}];
[self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(30);
make.width.mas_equalTo(@50);
make.centerY.mas_equalTo(self.customCountView);
make.right.mas_equalTo(self.customCountView).offset(-15);
}];
}
- (void)initEvents {
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTagRecharge:)];
[self.rechargeStackView addGestureRecognizer:tap];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat height = CGRectGetMinY(keyboardRect) - KScreenHeight;
if (height < 0) {
[UIView animateWithDuration:0.25 animations:^{
CGRect rect = self.customCountView.superview.superview.superview.frame;
rect.origin.y = height;
self.customCountView.superview.superview.superview.frame = rect;
}];
}
}
//键盘隐藏
- (void)keyboardWillHidden:(NSNotification *)notification {
[UIView animateWithDuration:0.25 animations:^{
CGRect rect = self.customCountView.superview.superview.superview.frame;
rect.origin.y = 0;
self.customCountView.superview.superview.superview.frame = rect;
}];
self.customCountView.hidden = YES;
self.editTextFiled.text = @"";
}
- (void)addNotificationCenter {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)updateCountStatus {
self.arrowButton.selected = !self.arrowButton.selected;
self.giftCountView.hidden = !self.arrowButton.selected;
}
#pragma mark - XPGiftCountViewDelegate
- (void)xPGiftCountView:(XPGiftCountView *)view didClickItem:(XPGiftCountModel *)model {
self.arrowButton.selected = NO;
self.giftCountView.hidden= YES;
self.giftCountModel = model;
if (model.isCustomCount) {
if (![[self.superview.superview subviews] containsObject:self.customCountView]) {
[self.superview.superview addSubview:self.customCountView];
[self.customCountView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(self.superview.superview);
make.height.mas_equalTo(40);
}];
}
self.customCountView.hidden = NO;
[self.editTextFiled becomeFirstResponder];
}
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSString *countStr = [self.editTextFiled.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSInteger count = [countStr integerValue];
if (count > 9999) {
return NO;
}
[self.editTextFiled resignFirstResponder];
self.editTextFiled.text = @"";
self.giftCountModel.giftNumber = textField.text;
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *regex =@"[0-9]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
if ([pred evaluateWithObject:string]) {
return YES;
}
return NO;
}
#pragma mark - Getters And Setters
- (void)setDrawGiftCount:(NSInteger)drawGiftCount {
_drawGiftCount = drawGiftCount;
if (_drawGiftCount >= 10) {
self.sendGiftButton.selected = NO;
self.sendOperationView.layer.borderWidth = 1;
} else {
self.sendGiftButton.selected = YES;
self.sendOperationView.layer.borderWidth = 0;
}
}
- (void)setSendButtonIsEnable:(BOOL)sendButtonIsEnable {
_sendButtonIsEnable = sendButtonIsEnable;
[UIView animateWithDuration:0.3 animations:^{
self.sendGiftButton.enabled = self.sendButtonIsEnable;
}];
}
- (void)setIsShowFirstRecharge:(BOOL)isShowFirstRecharge {
self.firstRechargeButton.hidden = !isShowFirstRecharge;
}
- (void)setType:(GiftSegmentType)type {
_type = type;
if (!self.giftCountView.hidden) {
self.giftCountView.hidden = YES;
self.arrowButton.selected = NO;
} else {
if (_type == GiftSegmentType_Graffiti) {
self.countLabel.hidden = YES;
self.arrowButton.hidden = YES;
} else {
self.countLabel.hidden = NO;
self.arrowButton.hidden = NO;
}
}
self.giftCountView.segmentType = type;
}
- (void)setGiftCountModel:(XPGiftCountModel *)giftCountModel {
_giftCountModel = giftCountModel;
self.arrowButton.selected = NO;
if (_giftCountModel) {
self.countLabel.text = _giftCountModel.giftNumber;
}
}
- (void)setWalletInfoModel:(WalletInfoModel *)walletInfoModel {
_walletInfoModel = walletInfoModel;
if (_walletInfoModel) {
NSString * diamonds = _walletInfoModel.diamonds;
NSString * title = [NSString stringWithFormat:@"余额:%@", diamonds];
NSMutableAttributedString * attribute = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:[ThemeColor giftBalanceColor]}];
[attribute addAttribute:NSForegroundColorAttributeName value:[ThemeColor giftCountTitleColor] range:[title rangeOfString:diamonds]];
self.balanceLabel.attributedText = attribute;
}
}
- (UILabel *)balanceLabel {
if (!_balanceLabel) {
_balanceLabel = [[UILabel alloc] init];
_balanceLabel.textColor = [ThemeColor mainTextColor];
_balanceLabel.font = [UIFont systemFontOfSize:13];
}
return _balanceLabel;
}
- (UIStackView *)rechargeStackView {
if (!_rechargeStackView) {
_rechargeStackView = [[UIStackView alloc] init];
_rechargeStackView.axis = UILayoutConstraintAxisHorizontal;
_rechargeStackView.distribution = UIStackViewDistributionFill;
_rechargeStackView.alignment = UIStackViewAlignmentCenter;
_rechargeStackView.spacing = 1;
}
return _rechargeStackView;
}
- (UILabel *)rechargeLabel {
if (!_rechargeLabel) {
_rechargeLabel = [[UILabel alloc] init];
_rechargeLabel.text = @"充值";
_rechargeLabel.font = [UIFont systemFontOfSize:12];
_rechargeLabel.textColor = [ThemeColor giftRechargeColor];
}
return _rechargeLabel;
}
- (UIImageView *)rechageImageView {
if (!_rechageImageView) {
_rechageImageView = [[UIImageView alloc] init];
_rechageImageView.userInteractionEnabled = YES;
_rechageImageView.image = [UIImage imageNamed:@"gift_bar_recharge_arrow"];
}
return _rechageImageView;
}
- (UIStackView *)sendOperationView {
if (!_sendOperationView) {
_sendOperationView = [[UIStackView alloc] init];
_sendOperationView.axis = UILayoutConstraintAxisHorizontal;
_sendOperationView.distribution = UIStackViewDistributionFill;
_sendOperationView.alignment = UIStackViewAlignmentCenter;
_sendOperationView.spacing = 0;
_sendOperationView.layer.masksToBounds = YES;
_sendOperationView.layer.cornerRadius = 15;
_sendOperationView.layer.borderColor = [ThemeColor appMainColor].CGColor;
_sendOperationView.layer.borderWidth = 1;
}
return _sendOperationView;
}
- (UILabel *)countLabel {
if (!_countLabel) {
_countLabel = [[UILabel alloc] init];
_countLabel.textAlignment = NSTextAlignmentCenter;
_countLabel.textColor = [ThemeColor giftCountTitleColor];
_countLabel.text = @"1";
_countLabel.font = [UIFont systemFontOfSize:13];
_countLabel.textAlignment = NSTextAlignmentCenter;
}
return _countLabel;
}
- (UIButton *)arrowButton {
if (!_arrowButton) {
_arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_arrowButton setImage:[UIImage imageNamed:@"gift_bar_count_arrow"] forState:UIControlStateNormal];
[_arrowButton setImage:[UIImage imageNamed:@"gift_bar_count_up_arrow"] forState:UIControlStateSelected];
[_arrowButton addTarget:self action:@selector(arrowButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _arrowButton;
}
- (UIButton *)sendGiftButton {
if (!_sendGiftButton) {
_sendGiftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_sendGiftButton setTitle:@"赠送" forState:UIControlStateNormal];
[_sendGiftButton setTitleColor:[ThemeColor mainTextColor] forState:UIControlStateNormal];
[_sendGiftButton setTitle:@"赠送中..." forState:UIControlStateDisabled];
[_sendGiftButton setTitleColor:[ThemeColor disableButtonTextColor] forState:UIControlStateDisabled];
[_sendGiftButton setTitle:@"赠送" forState:UIControlStateSelected];
[_sendGiftButton setTitleColor:[ThemeColor disableButtonTextColor] forState:UIControlStateSelected];
[_sendGiftButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
[_sendGiftButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor disableButtonColor], [ThemeColor disableButtonColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateDisabled];
[_sendGiftButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor disableButtonColor], [ThemeColor disableButtonColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateSelected];
_sendGiftButton.titleLabel.font = [UIFont systemFontOfSize:12];
_sendGiftButton.layer.masksToBounds = YES;
_sendGiftButton.layer.cornerRadius = 15;
[_sendGiftButton addTarget:self action:@selector(sendButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _sendGiftButton;
}
- (UITextField *)editTextFiled {
if (!_editTextFiled) {
_editTextFiled = [[UITextField alloc] init];
NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:@"请输入赠送数额" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17], NSForegroundColorAttributeName:[ThemeColor secondTextColor]}];
_editTextFiled.attributedPlaceholder = attribute;
_editTextFiled.borderStyle = UITextBorderStyleNone;
_editTextFiled.returnKeyType = UIReturnKeyDone;
_editTextFiled.delegate = self;
_editTextFiled.textColor = [ThemeColor secondTextColor];
_editTextFiled.keyboardType = UIKeyboardTypeNumberPad;
}
return _editTextFiled;
}
- (UIButton *)sureButton {
if (!_sureButton) {
_sureButton = [[UIButton alloc] init];
[_sureButton setTitle:@"确定" forState:UIControlStateNormal];
_sureButton.titleLabel.textColor = [ThemeColor mainTextColor];
_sureButton.titleLabel.font = [UIFont systemFontOfSize:15];
_sureButton.backgroundColor = [ThemeColor appMainColor];
_sureButton.layer.cornerRadius = 5.0;
_sureButton.layer.masksToBounds = YES;
[_sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _sureButton;
}
- (UIView *)customCountView {
if (!_customCountView) {
_customCountView = [[UIView alloc] init];
_customCountView.backgroundColor = [UIColor whiteColor];
}
return _customCountView;
}
- (XPGiftCountView *)giftCountView {
if (!_giftCountView) {
_giftCountView = [[XPGiftCountView alloc] init];
_giftCountView.delegate = self;
_giftCountView.hidden = YES;
}
return _giftCountView;
}
- (UIButton *)firstRechargeButton {
if (!_firstRechargeButton) {
_firstRechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_firstRechargeButton setImage:[UIImage imageNamed:@"gift_first_recharge_bg"] forState:UIControlStateNormal];
[_firstRechargeButton setImage:[UIImage imageNamed:@"gift_first_recharge_bg"] forState:UIControlStateSelected];
[_firstRechargeButton addTarget:self action:@selector(firstRechargeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}
return _firstRechargeButton;
}
@end