// // 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" ///Model #import "GiftInfoModel.h" #import "GiftReceiveInfoModel.h" ///View #import "XPGiftItemCollectionViewCell.h" #import "XPGiftEmptyCollectionViewCell.h" #import "XPGiftWeekStarCollectionViewCell.h" #import "XPGiftCollectionViewFlowLayout.h" @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) 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) NSAttributedString *totalAttribute; ///当前展示的数据的类型 @property (nonatomic,assign) GiftSegmentType segmentType; ///最后一次选中的礼物 @property (nonatomic,strong) GiftInfoModel *lastSelectGift; @property (nonatomic, assign) NSInteger selectIndex; @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; 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.segmentStackView addArrangedSubview:self.normalGiftButton]; [self.segmentStackView addArrangedSubview:self.luckyGiftButton]; [self.segmentStackView addArrangedSubview:self.nobleGiftButton]; [self.segmentStackView addArrangedSubview:self.weekStarButton]; if(self.segmentType != GiftSegmentType_Pack) { [self didClickGiftSegmentAction:self.normalGiftButton]; } } else { [self.normalGiftButton removeFromSuperview]; [self.luckyGiftButton removeFromSuperview]; [self.nobleGiftButton removeFromSuperview]; [self.weekStarButton removeFromSuperview]; [self.segmentStackView addArrangedSubview:self.graffitiButton]; 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.luckyGiftButton]; [self.segmentStackView addArrangedSubview:self.nobleGiftButton]; [self.segmentStackView addArrangedSubview:self.weekStarButton]; ///礼物 [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); 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 secondTextColor]}]; [str appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@钻石",[self countFormatCoinStr:giftTotal]] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor whiteColor]}]]; 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; if (self.segmentType == GiftSegmentType_WeekStar) { if (self.delegate && [self.delegate respondsToSelector:@selector(xpGiftInfoViewDidClickWeekStarGift:)]) { [self.delegate xpGiftInfoViewDidClickWeekStarGift:giftInfo]; } } } // 根据礼物id查找 - (GiftInfoModel *)findGiftInfoByGiftId:(NSInteger)giftId { for (int i=0; i 0) { [self resetSelectGift:self.datasource]; GiftInfoModel * giftInfo= [self.datasource objectAtIndex: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)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.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; default: [self.datasource addObjectsFromArray:self.giftArray]; break; } if (self.datasource.count > 0) { if (self.defaultSelectGiftId.length) { for (GiftInfoModel * gift in self.datasource) { if (gift.giftId == [self.defaultSelectGiftId integerValue]) { [self dealSelectGift:gift]; self.selectIndex = [self.datasource indexOfObject:gift]; } } } else { GiftInfoModel * gift = [self.datasource firstObject]; [self dealSelectGift:gift]; } } NSInteger page = 0; if (self.datasource.count % 8 == 0) { //刚好满页 page = self.datasource.count / 8; } else { page = self.datasource.count / 8 + 1; } self.pageController.hidden = page <= 1 || _segmentType == GiftSegmentType_WeekStar; [self.pageController setNumberOfPages:page]; self.pageController.currentPage = 0; [self.giftcollectionView reloadData]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickSegment:)]) { [self.delegate xPGiftInfoView:self didClickSegment:_segmentType]; } if (self.segmentType != GiftSegmentType_Graffiti && self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickItem:type:)]) { [self.delegate xPGiftInfoView:self didClickItem:self.lastSelectGift type:_segmentType]; } } - (void)setNormalOriginArray:(NSArray *)normalOriginArray { _normalOriginArray = normalOriginArray; NSMutableArray * luckyArray = [NSMutableArray array]; NSMutableArray * normaleArray = [NSMutableArray array]; NSMutableArray * nobleArray = [NSMutableArray array]; NSMutableArray * weekStarArray = [NSMutableArray array]; NSMutableArray * graffiti = [NSMutableArray array]; [_normalOriginArray enumerateObjectsUsingBlock:^(GiftInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if (obj.giftType == GiftType_Lucky) { [luckyArray addObject:obj]; } else if(obj.giftType == GiftType_Game) { [normaleArray addObject:obj]; } else if (obj.giftType == GiftType_Noble) { [nobleArray addObject:obj]; } else if (obj.giftType == GiftType_WeekStar) { [weekStarArray addObject:obj]; } else if (obj.giftType == GiftType_Graffiti) { [graffiti addObject:obj]; } }]; self.giftArray = normaleArray; self.giftLuckyArray = luckyArray; self.giftNobleArray = nobleArray; self.giftWeekStarArray = weekStarArray; self.giftGraffitiArray = graffiti; if (self.defaultSelectGiftId.length) { for (GiftInfoModel *gift in self.normalOriginArray) { if (gift.giftId == [self.defaultSelectGiftId integerValue]) { self.segmentType = GiftSegmentType_Normal; break; } } for (GiftInfoModel *gift in self.giftLuckyArray) { if (gift.giftId == [self.defaultSelectGiftId integerValue]) { self.segmentType = GiftSegmentType_Lucky; break; } } for (GiftInfoModel *gift in self.giftNobleArray) { if (gift.giftId == [self.defaultSelectGiftId integerValue]) { self.segmentType = GiftSegmentType_Noble; break; } } for (GiftInfoModel *gift in self.giftWeekStarArray) { if (gift.giftId == [self.defaultSelectGiftId integerValue]) { self.segmentType = GiftSegmentType_WeekStar; break; } } for (GiftInfoModel *gift in self.giftGraffitiArray) { if (gift.giftId == [self.defaultSelectGiftId integerValue]) { self.segmentType = GiftSegmentType_Graffiti; break; } } } else { self.segmentType = GiftSegmentType_Normal; } } - (void)setPackOriginArray:(NSArray *)packOriginArray { _packOriginArray = packOriginArray; [self createPackTotalValueAttribute]; } - (void)setCurUserNobleLevel:(NSInteger)curUserNobleLevel { _curUserNobleLevel = curUserNobleLevel; [self.giftcollectionView reloadData]; } - (void)setUsingplaceType:(SendGiftType)usingplaceType { _usingplaceType = usingplaceType; if (_usingplaceType == SendGiftType_User) { self.luckyGiftButton.hidden = YES; self.graffitiButton.hidden = YES; } } - (void)setDefaultSelectGiftId:(NSString *)defaultSelectGiftId { _defaultSelectGiftId = defaultSelectGiftId; } - (UIStackView *)segmentStackView { if (!_segmentStackView) { _segmentStackView = [[UIStackView alloc] init]; _segmentStackView.axis = UILayoutConstraintAxisHorizontal; _segmentStackView.distribution = UIStackViewDistributionFill; _segmentStackView.alignment = UIStackViewAlignmentCenter; _segmentStackView.spacing = 10; _segmentStackView.backgroundColor = [UIColor clearColor]; } return _segmentStackView; } - (UIStackView *)giftStackView { if (!_giftStackView) { _giftStackView = [[UIStackView alloc] init]; _giftStackView.axis = UILayoutConstraintAxisVertical; _giftStackView.distribution = UIStackViewDistributionFill; _giftStackView.alignment = UIStackViewAlignmentFill; _giftStackView.spacing = 0; } return _giftStackView; } - (UIButton *)normalGiftButton { if (!_normalGiftButton) { _normalGiftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_normalGiftButton setTitle:@"礼物" forState:UIControlStateNormal]; [_normalGiftButton setTitleColor:[ThemeColor giftSegmentSelectTitleColor] forState:UIControlStateSelected]; [_normalGiftButton setTitleColor:[ThemeColor giftSegmentNormalTitleColor] forState:UIControlStateNormal]; _normalGiftButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _normalGiftButton.tag = GiftSegmentType_Normal; _normalGiftButton.selected = YES; [_normalGiftButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside]; } return _normalGiftButton; } - (UIButton *)luckyGiftButton { if (!_luckyGiftButton) { _luckyGiftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_luckyGiftButton setTitle:@"幸运礼物" forState:UIControlStateNormal]; [_luckyGiftButton setTitleColor:[ThemeColor giftSegmentSelectTitleColor] forState:UIControlStateSelected]; [_luckyGiftButton setTitleColor:[ThemeColor giftSegmentNormalTitleColor] forState:UIControlStateNormal]; _luckyGiftButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _luckyGiftButton.tag = GiftSegmentType_Lucky; _luckyGiftButton.selected = NO; [_luckyGiftButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside]; } return _luckyGiftButton; } - (UIButton *)nobleGiftButton { if (!_nobleGiftButton) { _nobleGiftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_nobleGiftButton setTitle:@"贵族" forState:UIControlStateNormal]; [_nobleGiftButton setTitleColor:[ThemeColor giftSegmentSelectTitleColor] forState:UIControlStateSelected]; [_nobleGiftButton setTitleColor:[ThemeColor giftSegmentNormalTitleColor] forState:UIControlStateNormal]; _nobleGiftButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _nobleGiftButton.tag = GiftSegmentType_Noble; _nobleGiftButton.selected = NO; [_nobleGiftButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside]; } return _nobleGiftButton; } - (UIButton *)weekStarButton { if (!_weekStarButton) { _weekStarButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_weekStarButton setTitle:@"周星礼物" forState:UIControlStateNormal]; [_weekStarButton setTitleColor:[ThemeColor giftSegmentSelectTitleColor] forState:UIControlStateSelected]; [_weekStarButton setTitleColor:[ThemeColor giftSegmentNormalTitleColor] forState:UIControlStateNormal]; _weekStarButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _weekStarButton.tag = GiftSegmentType_WeekStar; _weekStarButton.selected = NO; [_weekStarButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside]; } return _weekStarButton; } - (UIButton *)packGiftButton { if (!_packGiftButton) { _packGiftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_packGiftButton setTitle:@"背包" forState:UIControlStateNormal]; [_packGiftButton setTitleColor:[ThemeColor giftSegmentSelectTitleColor] forState:UIControlStateSelected]; [_packGiftButton setTitleColor:[ThemeColor giftSegmentNormalTitleColor] forState:UIControlStateNormal]; _packGiftButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _packGiftButton.tag = GiftSegmentType_Pack; _packGiftButton.selected = NO; [_packGiftButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside]; } return _packGiftButton; } - (UIButton *)graffitiButton { if (!_graffitiButton) { _graffitiButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_graffitiButton setTitle:@"涂鸦礼物" forState:UIControlStateNormal]; [_graffitiButton setTitleColor:[ThemeColor giftSegmentSelectTitleColor] forState:UIControlStateSelected]; [_graffitiButton setTitleColor:[ThemeColor giftSegmentNormalTitleColor] forState:UIControlStateNormal]; _graffitiButton.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightSemibold]; _graffitiButton.tag = GiftSegmentType_Graffiti; _graffitiButton.selected = NO; [_graffitiButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside]; } return _graffitiButton; } - (UILabel *)totalValueLabel { if (!_totalValueLabel) { _totalValueLabel = [[UILabel alloc] init]; } return _totalValueLabel; } - (UICollectionView *)giftcollectionView{ if (!_giftcollectionView) { XPGiftCollectionViewFlowLayout *layout = [[XPGiftCollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 5; layout.minimumInteritemSpacing = 5; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); _giftcollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _giftcollectionView.dataSource = self; _giftcollectionView.delegate = self; _giftcollectionView.backgroundColor = [UIColor clearColor]; _giftcollectionView.pagingEnabled = YES; [_giftcollectionView registerClass:[XPGiftItemCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftItemCollectionViewCell class])]; [_giftcollectionView registerClass:[XPGiftEmptyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftEmptyCollectionViewCell class])]; [_giftcollectionView registerClass:[XPGiftWeekStarCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftWeekStarCollectionViewCell class])]; _giftcollectionView.showsHorizontalScrollIndicator = NO; } return _giftcollectionView; } - (UIPageControl *)pageController { if (!_pageController) { _pageController = [[UIPageControl alloc] init]; _pageController.currentPageIndicatorTintColor = [ThemeColor giftPageIndicatorColor]; } return _pageController; } - (NSMutableArray *)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