// // XPGiftCountView.m // xplan-ios // // Created by 冯硕 on 2021/11/11. // #import "XPGiftCountView.h" ///Third #import #import "NSArray+Safe.h" ///Model #import "XPGiftCountModel.h" ///View #import "XPGiftCountCollectionViewCell.h" @interface XPGiftCountView () //列表 @property (nonatomic,strong) UICollectionView *countCollectionView; ///礼物数量数据源 @property (nonatomic, strong) NSArray *giftCountArray; /// @property (nonatomic,strong) UIVisualEffectView *effectView; @end @implementation XPGiftCountView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { // 设置毛玻璃效果 self.layer.cornerRadius = 12; self.layer.masksToBounds = YES; UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; self.effectView = effectView; [self addSubview:effectView]; self.giftCountArray = [self normalGiftCountDataArray]; [self addSubview:self.countCollectionView]; } - (void)initSubViewConstraints { [self mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(0); }]; [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self); }]; [self.countCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self); }]; } ///普通礼物的 个数的数据源 - (NSArray *)normalGiftCountDataArray { NSMutableArray * datasource = [NSMutableArray array]; XPGiftCountModel *_otherDic = [XPGiftCountModel initCountModel:@"其他数额" giftNumber:@""]; _otherDic.isCustomCount = YES; NSArray * array = [self commonGiftCountDataArray]; [datasource addObjectsFromArray:array]; [datasource addObject:_otherDic]; return [datasource copy]; } ///背包礼物的数据源 - (NSArray *)packGiftCountDataArray { NSMutableArray * datasource = [NSMutableArray array]; XPGiftCountModel *_allDic = [XPGiftCountModel initCountModel:@"全部" giftNumber:@"all"]; _allDic.isTotal = YES; // 其他数额 XPGiftCountModel *_otherDic = [XPGiftCountModel initCountModel:@"其他数额" giftNumber:@""]; _otherDic.isCustomCount = YES; NSArray * array = [self commonGiftCountDataArray]; [datasource addObjectsFromArray:array]; [datasource addObject:_allDic]; [datasource addObject:_otherDic]; return [datasource copy]; } ///一些公用的数据 - (NSArray *)commonGiftCountDataArray { XPGiftCountModel *_1Dic = [XPGiftCountModel initCountModel:@"一心一意" giftNumber:@"1"]; XPGiftCountModel *_10Dic = [XPGiftCountModel initCountModel:@"十全十美" giftNumber:@"10"]; XPGiftCountModel *_66Dic = [XPGiftCountModel initCountModel:@"一切顺利" giftNumber:@"66"]; XPGiftCountModel *_99Dic = [XPGiftCountModel initCountModel:@"长长久久" giftNumber:@"99"]; XPGiftCountModel *_188Dic = [XPGiftCountModel initCountModel:@"要抱抱" giftNumber:@"188"]; XPGiftCountModel *_520Dic = [XPGiftCountModel initCountModel:@"我爱你" giftNumber:@"520"]; XPGiftCountModel *_1314Dic = [XPGiftCountModel initCountModel:@"一生一世" giftNumber:@"1314"]; NSArray * array = @[_1Dic, _10Dic, _66Dic, _99Dic,_188Dic,_520Dic, _1314Dic]; return array; } #pragma mark - UICollectionViewDelegate && UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.giftCountArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { XPGiftCountCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftCountCollectionViewCell class]) forIndexPath:indexPath]; XPGiftCountModel *countItem = [self.giftCountArray safeObjectAtIndex1:indexPath.item]; cell.countModel = countItem; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [collectionView deselectItemAtIndexPath:indexPath animated:YES]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftCountView:didClickItem:)]) { XPGiftCountModel *countItem = [self.giftCountArray safeObjectAtIndex1:indexPath.item]; [self.delegate xPGiftCountView:self didClickItem:countItem]; } } - (void)setSegmentType:(GiftSegmentType)segmentType { _segmentType = segmentType; if (_segmentType == GiftSegmentType_Pack) { self.giftCountArray = [self packGiftCountDataArray]; } else { self.giftCountArray = [self normalGiftCountDataArray]; } if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftCountView:didClickItem:)]) { XPGiftCountModel *countItem = [self.giftCountArray safeObjectAtIndex1:0]; [self.delegate xPGiftCountView:self didClickItem:countItem]; } [self mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(28 * self.giftCountArray.count + 10); }]; [self.countCollectionView reloadData]; } #pragma mark - Getters And Setters - (UICollectionView *)countCollectionView{ if (!_countCollectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(135, 28); layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; layout.sectionInset = UIEdgeInsetsMake(3, 0, 3, 0); _countCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _countCollectionView.dataSource = self; _countCollectionView.delegate = self; _countCollectionView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]; [_countCollectionView registerClass:[XPGiftCountCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftCountCollectionViewCell class])]; } return _countCollectionView; } @end