440 lines
16 KiB
Objective-C
440 lines
16 KiB
Objective-C
//
|
||
// XPGiftMiddleView.m
|
||
// xplan-ios
|
||
//
|
||
// Created by 冯硕 on 2021/11/9.
|
||
//
|
||
|
||
#import "XPGiftInfoView.h"
|
||
///Third
|
||
#import <Masonry/Masonry.h>
|
||
///Tool
|
||
#import "XPMacro.h"
|
||
#import "ThemeColor+SendGift.h"
|
||
///Model
|
||
#import "GiftInfoModel.h"
|
||
#import "GiftReceiveInfoModel.h"
|
||
///View
|
||
#import "XPGiftItemCollectionViewCell.h"
|
||
#import "XPGiftEmptyCollectionViewCell.h"
|
||
#import "XPGiftCollectionViewFlowLayout.h"
|
||
@interface XPGiftInfoView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||
///
|
||
@property (nonatomic,strong) UIStackView *segmentStackView;
|
||
///普通礼物
|
||
@property (nonatomic,strong) UIButton *normalGiftButton;
|
||
///背包礼物
|
||
@property (nonatomic,strong) UIButton *packGiftButton;
|
||
///幸运礼物
|
||
@property (nonatomic,strong) UIButton *luckyGiftButton;
|
||
///占位的
|
||
@property (nonatomic,strong) UIView * segmentPlaceView;
|
||
///玩法规则的
|
||
@property (nonatomic,strong) UIButton *playRuleButton;
|
||
///背包总价值
|
||
@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 <GiftInfoModel *> *datasource;
|
||
///普通礼物的数据源
|
||
@property (nonatomic,strong) NSArray<GiftInfoModel *> *giftArray;
|
||
/////幸运礼物的数据源
|
||
@property (nonatomic,strong) NSArray<GiftInfoModel *> *giftLuckyArray;
|
||
/////背包礼物的数据源
|
||
@property (nonatomic,strong) NSArray<GiftInfoModel *> *giftPackArray;
|
||
///总的价值
|
||
@property (nonatomic,strong) NSAttributedString *totalAttribute;
|
||
///当前展示的数据的类型
|
||
@property (nonatomic,assign) GiftSegmentType segmentType;
|
||
///最后一次选中的礼物
|
||
@property (nonatomic,strong) GiftInfoModel *lastSelectGift;
|
||
@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.packGiftButton.selected = NO;
|
||
sender.selected = !sender.selected;
|
||
self.segmentType = sender.tag;
|
||
}
|
||
|
||
- (void)playRuleButtonAction:(UIButton *)sender {
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftInfoView:didClickPlayRule:)]) {
|
||
[self.delegate xPGiftInfoView:self didClickPlayRule:self.lastSelectGift.giftExplainUrl];
|
||
}
|
||
}
|
||
|
||
#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];
|
||
}
|
||
|
||
#pragma mark - Private Method
|
||
- (void)initSubViews {
|
||
|
||
[self addSubview:self.segmentStackView];
|
||
[self addSubview:self.giftStackView];
|
||
///分段控制
|
||
[self.segmentStackView addArrangedSubview:self.normalGiftButton];
|
||
[self.segmentStackView addArrangedSubview:self.luckyGiftButton];
|
||
[self.segmentStackView addArrangedSubview:self.packGiftButton];
|
||
[self.segmentStackView addArrangedSubview:self.segmentPlaceView];
|
||
[self.segmentStackView addArrangedSubview:self.playRuleButton];
|
||
[self.segmentStackView addArrangedSubview:self.totalValueLabel];
|
||
///礼物
|
||
[self.giftStackView addArrangedSubview:self.giftcollectionView];
|
||
[self.giftStackView addArrangedSubview:self.pageController];
|
||
}
|
||
|
||
- (void)initSubViewConstraints {
|
||
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.height.mas_equalTo(40 + 105 * 2 + 10 + 10);
|
||
}];
|
||
|
||
|
||
[self.segmentStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.left.right.mas_equalTo(self).inset(15);
|
||
make.top.mas_equalTo(self);
|
||
make.height.mas_equalTo(40);
|
||
}];
|
||
|
||
[self.giftStackView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(self.segmentStackView.mas_bottom);
|
||
make.left.right.mas_equalTo(self);
|
||
make.height.mas_equalTo(105 * 2 + 20);
|
||
}];
|
||
|
||
[self.pageController mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.height.mas_equalTo(10);
|
||
}];
|
||
}
|
||
|
||
- (void)resetSelectGift:(NSArray<GiftInfoModel *> *)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:11],NSForegroundColorAttributeName:[ThemeColor giftPlayRuleColor]}];
|
||
[str appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@钻石",[self countFormatCoinStr:giftTotal]] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[ThemeColor mainTextColor]}]];
|
||
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;
|
||
}
|
||
self.playRuleButton.hidden = giftInfo.giftExplainUrl.length <= 0;
|
||
giftInfo.isSelected = YES;
|
||
}
|
||
|
||
// 根据礼物id查找
|
||
- (GiftInfoModel *)findGiftInfoByGiftId:(NSInteger)giftId {
|
||
for (int i=0; i<self.datasource.count ; i++) {
|
||
GiftInfoModel *giftInfo = [self.datasource objectAtIndex:i];
|
||
if (giftInfo.giftId == giftId) {
|
||
return giftInfo;
|
||
}
|
||
}
|
||
return nil;
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - scrollviewdelegate
|
||
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
||
CGFloat offX = scrollView.contentOffset.x;
|
||
CGFloat width = CGRectGetWidth(scrollView.frame);
|
||
self.pageController.currentPage = ceilf(offX/width);
|
||
}
|
||
|
||
#pragma mark - UICollectionViewDelegate And UICollectionDatasource
|
||
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||
if (self.segmentType == GiftSegmentType_Pack && self.datasource.count == 0) {
|
||
return CGSizeMake(KScreenWidth, 105 * 2 + 10);
|
||
}
|
||
CGFloat itemWidth = (CGFloat)(KScreenWidth - 10 - 5 * 3) / (CGFloat)4;
|
||
return CGSizeMake(itemWidth, 105);
|
||
}
|
||
|
||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
||
if (self.segmentType == GiftSegmentType_Pack && self.datasource.count == 0) {
|
||
return 1;
|
||
}
|
||
return self.datasource.count;
|
||
}
|
||
|
||
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||
if (self.segmentType ==GiftSegmentType_Pack && self.datasource.count == 0) {
|
||
XPGiftEmptyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftEmptyCollectionViewCell class]) forIndexPath:indexPath];
|
||
return cell;
|
||
}
|
||
XPGiftItemCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftItemCollectionViewCell class]) forIndexPath:indexPath];
|
||
GiftInfoModel * giftInfo;
|
||
giftInfo= [self.datasource objectAtIndex:indexPath.item];
|
||
cell.giftInfo = giftInfo;
|
||
return cell;
|
||
}
|
||
|
||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
||
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
|
||
if (self.datasource.count > 0) {
|
||
[self resetSelectGift:self.datasource];
|
||
GiftInfoModel * giftInfo= [self.datasource objectAtIndex:indexPath.item];
|
||
[self dealSelectGift:giftInfo];
|
||
[self.giftcollectionView reloadData];
|
||
|
||
}
|
||
}
|
||
|
||
#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.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_Pack:
|
||
{
|
||
[self.datasource addObjectsFromArray:self.packOriginArray];
|
||
self.totalValueLabel.hidden = NO;
|
||
self.totalValueLabel.attributedText= self.totalAttribute;
|
||
}
|
||
break;
|
||
default:
|
||
[self.datasource addObjectsFromArray:self.giftArray];
|
||
break;
|
||
}
|
||
if (self.datasource.count > 0) {
|
||
GiftInfoModel * gift = [self.datasource firstObject];
|
||
[self dealSelectGift:gift];
|
||
} else {
|
||
self.playRuleButton.hidden = YES;
|
||
}
|
||
NSUInteger page = self.datasource.count / 8;
|
||
self.pageController.hidden = page <= 0;
|
||
[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];
|
||
}
|
||
}
|
||
|
||
- (void)setNormalOriginArray:(NSArray *)normalOriginArray {
|
||
_normalOriginArray = normalOriginArray;
|
||
NSMutableArray * luckyArray = [NSMutableArray array];
|
||
NSMutableArray * normaleArray = [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];
|
||
}
|
||
}];
|
||
self.giftArray = normaleArray;
|
||
self.giftLuckyArray = luckyArray;
|
||
self.segmentType = GiftSegmentType_Normal;
|
||
}
|
||
|
||
- (void)setPackOriginArray:(NSArray *)packOriginArray {
|
||
_packOriginArray = packOriginArray;
|
||
[self createPackTotalValueAttribute];
|
||
}
|
||
|
||
|
||
- (UIStackView *)segmentStackView {
|
||
if (!_segmentStackView) {
|
||
_segmentStackView = [[UIStackView alloc] init];
|
||
_segmentStackView.axis = UILayoutConstraintAxisHorizontal;
|
||
_segmentStackView.distribution = UIStackViewDistributionFill;
|
||
_segmentStackView.alignment = UIStackViewAlignmentCenter;
|
||
_segmentStackView.spacing = 22;
|
||
_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:14];
|
||
_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:14];
|
||
_luckyGiftButton.tag = GiftSegmentType_Lucky;
|
||
_luckyGiftButton.selected = NO;
|
||
[_luckyGiftButton addTarget:self action:@ selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _luckyGiftButton;
|
||
}
|
||
|
||
- (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:14];
|
||
_packGiftButton.tag = GiftSegmentType_Pack;
|
||
_packGiftButton.selected = NO;
|
||
[_packGiftButton addTarget:self action:@selector(didClickGiftSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _packGiftButton;
|
||
}
|
||
|
||
|
||
- (UIButton *)playRuleButton {
|
||
if (!_playRuleButton) {
|
||
_playRuleButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
||
[_playRuleButton setTitle:@"玩法说明" forState:UIControlStateNormal];
|
||
[_playRuleButton setTitleColor:[ThemeColor giftPlayRuleColor] forState:UIControlStateNormal];
|
||
_playRuleButton.titleLabel.font = [UIFont systemFontOfSize:13];
|
||
[_playRuleButton setImage:[UIImage imageNamed:@"gift_info_rule"] forState:UIControlStateNormal];
|
||
[_playRuleButton addTarget:self action:@selector(playRuleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
|
||
_playRuleButton.hidden = YES;
|
||
}
|
||
return _playRuleButton;
|
||
}
|
||
|
||
- (UILabel *)totalValueLabel {
|
||
if (!_totalValueLabel) {
|
||
_totalValueLabel = [[UILabel alloc] init];
|
||
}
|
||
return _totalValueLabel;
|
||
}
|
||
|
||
- (UICollectionView *)giftcollectionView{
|
||
if (!_giftcollectionView) {
|
||
XPGiftCollectionViewFlowLayout *layout = [[XPGiftCollectionViewFlowLayout alloc] init];
|
||
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
|
||
layout.minimumLineSpacing = 5;
|
||
layout.minimumInteritemSpacing = 10;
|
||
layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 5);
|
||
_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])];
|
||
}
|
||
return _giftcollectionView;
|
||
}
|
||
|
||
|
||
- (UIPageControl *)pageController {
|
||
if (!_pageController) {
|
||
_pageController = [[UIPageControl alloc] init];
|
||
_pageController.currentPageIndicatorTintColor = [ThemeColor giftPageIndicatorColor];
|
||
}
|
||
return _pageController;
|
||
}
|
||
|
||
- (UIView *)segmentPlaceView {
|
||
if (!_segmentPlaceView) {
|
||
_segmentPlaceView = [[UIView alloc] init];
|
||
_segmentPlaceView.backgroundColor = [UIColor clearColor];
|
||
}
|
||
return _segmentPlaceView;
|
||
}
|
||
|
||
|
||
- (NSMutableArray<GiftInfoModel *> *)datasource {
|
||
if (!_datasource) {
|
||
_datasource = [NSMutableArray array];
|
||
}
|
||
return _datasource;
|
||
}
|
||
|
||
|
||
@end
|