// // XPGiftMiddleView.m // xplan-ios // // Created by 冯硕 on 2021/11/9. // #import "XPGiftInfoView.h" ///Third #import ///Tool #import "XPMacro.h" #import "XPHtmlUrl.h" #import "ThemeColor+SendGift.h" #import "NSArray+Safe.h" ///Model #import "GiftInfoModel.h" #import "GiftReceiveInfoModel.h" #import "RoomInfoModel.h" ///View #import "XPGiftItemCollectionViewCell.h" #import "XPGiftEmptyCollectionViewCell.h" #import "XPGiftWeekStarCollectionViewCell.h" #import "XPGiftCollectionViewFlowLayout.h" static NSString *kSendGiftViewHidePunishNewIcon = @"kSendGiftViewHidePunishNewIcon";///是否隐藏惩罚礼物“新”标识 @interface XPGiftInfoView () ///滚动的容器 @property (nonatomic,strong) UIScrollView *scrollView; @property (nonatomic,strong) UIStackView *segmentStackView; ///普通礼物 @property (nonatomic,strong) UIButton *normalGiftButton; ///背包礼物 @property (nonatomic,strong) UIButton *packGiftButton; ///幸运礼物 @property (nonatomic,strong) UIButton *luckyGiftButton; ///贵族礼物 @property (nonatomic,strong) UIButton *nobleGiftButton; ///周星礼物 @property (nonatomic, strong) UIButton *weekStarButton; ///涂鸦礼物 @property (nonatomic,strong) UIButton *graffitiButton; ///惩罚礼物 @property (nonatomic, strong) UIButton *punishButton; ///新标识图标 @property (nonatomic, strong) UIImageView *freshIconImage; ///个播礼物 @property (nonatomic,strong) UIButton *anchorButton; ///背包总价值 @property (nonatomic,strong) UILabel *totalValueLabel; /// @property (nonatomic,strong) UIStackView *giftStackView; ///l礼物列表 @property (nonatomic,strong) UICollectionView *giftcollectionView; ///分页控件 @property (nonatomic, strong) UIPageControl *pageController; ///展示的数据源 @property (nonatomic,strong) NSMutableArray *datasource; ///普通礼物的数据源 @property (nonatomic,strong) NSArray *giftArray; /////幸运礼物的数据源 @property (nonatomic,strong) NSArray *giftLuckyArray; ///贵族礼物的数据源 @property (nonatomic, strong) NSArray *giftNobleArray; ///周星礼物的数据源 @property (nonatomic, strong) NSArray *giftWeekStarArray; /////背包礼物的数据源 @property (nonatomic,strong) NSArray *giftPackArray; ///涂鸦礼物数据源 @property (nonatomic,strong) NSArray *giftGraffitiArray; ///个播礼物数据源 @property (nonatomic,strong) NSArray *anchorArray; ///惩罚礼物数据源 @property (nonatomic, strong) NSArray *punishArray; ///总的价值 @property (nonatomic,strong) NSAttributedString *totalAttribute; ///当前展示的数据的类型 @property (nonatomic,assign) GiftSegmentType segmentType; ///最后一次选中的礼物 @property (nonatomic,strong) GiftInfoModel *lastSelectGift; @property (nonatomic, assign) NSInteger selectIndex; ///当前选中的page @property (nonatomic,assign) NSInteger selectCurrentPage; @end @implementation XPGiftInfoView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Response - (void)didClickGiftSegmentAction:(UIButton *)sender { self.normalGiftButton.selected = NO; self.luckyGiftButton.selected = NO; self.nobleGiftButton.selected = NO; self.packGiftButton.selected = NO; self.weekStarButton.selected = NO; self.graffitiButton.selected = NO; self.anchorButton.selected = NO; self.punishButton.selected = NO; sender.selected = !sender.selected; self.segmentType = sender.tag; } #pragma mark - Public Method - (void)updatePackSource:(GiftReceiveInfoModel *)giftReceiveInfo numberUser:(NSInteger)numberUser { GiftInfoModel * giftInfo = [self findGiftInfoByGiftId:giftReceiveInfo.giftId.integerValue]; giftInfo.count -= giftReceiveInfo.giftNum * numberUser; if (giftInfo.count == 0) { [self.datasource removeObject:giftInfo]; } [self.giftcollectionView reloadData]; } - (void)giftHeadTypeHadChange:(NSInteger)headType { if (headType == 1) { [self.graffitiButton removeFromSuperview]; [self.punishButton removeFromSuperview]; [self.freshIconImage removeFromSuperview]; self.freshIconImage = nil; [self.segmentStackView addArrangedSubview:self.normalGiftButton]; [self.segmentStackView addArrangedSubview:self.nobleGiftButton]; [self.segmentStackView addArrangedSubview:self.luckyGiftButton]; [self.segmentStackView addArrangedSubview:self.weekStarButton]; if(self.roomType == RoomType_Anchor) { [self.segmentStackView addArrangedSubview:self.anchorButton]; [self didClickGiftSegmentAction:self.anchorButton]; } if(self.segmentType != GiftSegmentType_Pack) { [self didClickGiftSegmentAction:self.normalGiftButton]; } } else { [self.normalGiftButton removeFromSuperview]; [self.luckyGiftButton removeFromSuperview]; [self.nobleGiftButton removeFromSuperview]; [self.weekStarButton removeFromSuperview]; [self.anchorButton removeFromSuperview]; [self.segmentStackView addArrangedSubview:self.graffitiButton]; [self.segmentStackView addArrangedSubview:self.punishButton]; BOOL hiden = [[NSUserDefaults standardUserDefaults] boolForKey:kSendGiftViewHidePunishNewIcon]; if(!hiden) { [self addSubview:self.freshIconImage]; [self.freshIconImage mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.punishButton.mas_right).mas_offset(-5); make.bottom.mas_equalTo(self.punishButton.mas_top).mas_offset(5); make.size.mas_equalTo(CGSizeMake(24, 10)); }]; } if(self.segmentType != GiftSegmentType_Pack) { [self didClickGiftSegmentAction:self.graffitiButton]; } } } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.scrollView]; [self addSubview:self.giftStackView]; [self addSubview:self.packGiftButton]; [self addSubview:self.totalValueLabel]; [self.scrollView addSubview:self.segmentStackView]; ///分段控制 [self.segmentStackView addArrangedSubview:self.normalGiftButton]; [self.segmentStackView addArrangedSubview:self.nobleGiftButton]; [self.segmentStackView addArrangedSubview:self.luckyGiftButton]; [self.segmentStackView addArrangedSubview:self.weekStarButton]; [self.segmentStackView addArrangedSubview:self.anchorButton]; ///礼物 [self.giftStackView addArrangedSubview:self.giftcollectionView]; [self.giftStackView addArrangedSubview:self.pageController]; } - (void)initSubViewConstraints { [self mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(30 + 108 * 2 + 10 + 10 + 15); }]; [self.segmentStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.leading.trailing.height.mas_equalTo(self.scrollView); }]; [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self).mas_offset(15); make.right.mas_equalTo(self.packGiftButton.mas_left); make.top.mas_equalTo(self); make.height.mas_equalTo(30); }]; [self.packGiftButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-15); make.centerY.height.mas_equalTo(self.scrollView); make.width.mas_equalTo(30); }]; [self.totalValueLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.packGiftButton.mas_left).mas_offset(-6); make.centerY.mas_equalTo(self.packGiftButton); }]; [self.giftStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.segmentStackView.mas_bottom).offset(10); make.left.right.mas_equalTo(self); make.height.mas_equalTo(108 * 2 + 20); }]; [self.pageController mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(10); }]; } - (void)resetSelectGift:(NSArray *)array { for (GiftInfoModel * gift in array) { gift.isSelected = NO; } } - (void)createPackTotalValueAttribute { __block NSInteger giftTotal = 0; [self.packOriginArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj isKindOfClass:GiftInfoModel.class]) { GiftInfoModel *gift = (GiftInfoModel *)obj; giftTotal += gift.count * gift.goldPrice; } }]; NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"总价值:" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[ThemeColor textThirdColor]}]; [str appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",[self countFormatCoinStr:giftTotal]] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[ThemeColor appMainColor]}]]; NSTextAttachment *attachImage = [[NSTextAttachment alloc] init]; attachImage.image = [UIImage imageNamed:@"gift_diamond"]; attachImage.bounds = CGRectMake(0, 0, 9, 9); [str appendAttributedString:[[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:attachImage]]]; self.totalAttribute = str; } /** 将数量格式化为字符串 万之后用xx.xxW显示并保留小数点2位,最多显示9999W+; @param number 数值 @return 格式化后的字符串 */ - (NSString *)countFormatCoinStr:(NSInteger)number { NSString *numStr = [NSString stringWithFormat:@"%li", number]; NSInteger num = number; if (num > 99990000) { numStr = @"9999W+"; } else if (num >= 10000) { CGFloat numF = num / 10000.0; numStr = [NSString stringWithFormat:@"%.2fW+", numF]; } return numStr; } - (void)dealSelectGift:(GiftInfoModel *)giftInfo { self.lastSelectGift = giftInfo; if (self.segmentType == GiftSegmentType_Pack) { giftInfo.sourceType = GiftSourceType_Pack; } else { giftInfo.sourceType = GiftSourceType_Normal; } giftInfo.isSelected = YES; } // 根据礼物id查找 - (GiftInfoModel *)findGiftInfoByGiftId:(NSInteger)giftId { for (int i=0; i 0) { [self resetSelectGift:self.datasource]; GiftInfoModel * giftInfo= [self.datasource safeObjectAtIndex1:indexPath.item]; [self dealSelectGift:giftInfo]; [self.giftcollectionView reloadData]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickItem:type:)]) { [self.delegate xPGiftInfoView:self didClickItem:giftInfo type:self.segmentType]; } } } #pragma mark - XPGiftWeekStarCollectionViewCellDelegate - (void)xPGiftWeekStarCollectionViewCell:(XPGiftWeekStarCollectionViewCell *)view didSelectGift:(GiftInfoModel *)giftInfo { [self dealSelectGift:giftInfo]; } #pragma mark - Getters And Setters - (void)setRoomType:(RoomType)roomType { _roomType = roomType; if (_roomType == RoomType_Anchor) { self.anchorButton.hidden = NO; } } - (void)setSegmentType:(GiftSegmentType)segmentType { if (segmentType == _segmentType) { return; } [_datasource removeAllObjects]; _segmentType = segmentType; [self resetSelectGift:self.giftArray]; [self resetSelectGift:self.packOriginArray]; [self resetSelectGift:self.giftLuckyArray]; [self resetSelectGift:self.giftNobleArray]; [self resetSelectGift:self.giftWeekStarArray]; [self resetSelectGift:self.giftGraffitiArray]; [self resetSelectGift:self.anchorArray]; [self resetSelectGift:self.punishArray]; self.totalValueLabel.hidden = YES; switch (_segmentType) { case GiftSegmentType_Normal: [self.datasource addObjectsFromArray:self.giftArray]; break; case GiftSegmentType_Lucky: [self.datasource addObjectsFromArray:self.giftLuckyArray]; break; case GiftSegmentType_Noble: { [self.datasource addObjectsFromArray:self.giftNobleArray]; } break; case GiftSegmentType_WeekStar: { [self.datasource addObjectsFromArray:self.giftWeekStarArray]; } break; case GiftSegmentType_Pack: { [self.datasource addObjectsFromArray:self.packOriginArray]; self.totalValueLabel.hidden = NO; self.totalValueLabel.attributedText= self.totalAttribute; } break; case GiftSegmentType_Graffiti: [self.datasource addObjectsFromArray:self.giftGraffitiArray]; break; case GiftSegmentType_Anchor: [self.datasource addObjectsFromArray:self.anchorArray]; break; case GiftSegmentType_Punish: { [self.datasource addObjectsFromArray:self.punishArray]; [self.freshIconImage removeFromSuperview]; self.freshIconImage = nil; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kSendGiftViewHidePunishNewIcon]; [[NSUserDefaults standardUserDefaults] synchronize]; } break; default: [self.datasource addObjectsFromArray:self.giftArray]; break; } NSInteger currentPage = 0; if (self.datasource.count > 0) { if (self.defaultSelectGiftId.length && (self.segmentType == GiftSegmentType_Normal | self.segmentType == GiftSegmentType_Pack)) { for (int i = 0 ; i *)datasource { if (!_datasource) { _datasource = [NSMutableArray array]; } return _datasource; } - (UIScrollView *)scrollView { if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.backgroundColor = [UIColor clearColor]; _scrollView.showsHorizontalScrollIndicator = NO; } return _scrollView; } @end