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

149 lines
4.9 KiB
Objective-C

//
// XPGiftWeekStarCollectionViewCell.m
// xplan-ios
//
// Created by 冯硕 on 2022/6/14.
//
#import "XPGiftWeekStarCollectionViewCell.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "NetImageView.h"
#import "ThemeColor.h"
#import "XPMacro.h"
#import "ThemeColor+SendGift.h"
#import "XPGiftCollectionViewFlowLayout.h"
///Model
#import "GiftInfoModel.h"
///View
#import "XPGiftItemCollectionViewCell.h"
@interface XPGiftWeekStarCollectionViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
///列表
@property (nonatomic,strong) UICollectionView *collectionView;
///分页
@property (nonatomic,strong) UIPageControl *pageController;
///选中的礼物
@property (nonatomic,strong) GiftInfoModel *selectGiftInfo;
@end
@implementation XPGiftWeekStarCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self.contentView addSubview:self.collectionView];
[self.contentView addSubview:self.pageController];
}
- (void)initSubViewConstraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self.contentView);
make.height.mas_equalTo(108 * 2);
}];
[self.pageController mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.collectionView.mas_bottom).offset(5);
make.left.right.mas_equalTo(self.contentView);
make.height.mas_equalTo(10);
}];
}
#pragma mark - UICollectionViewDelegate And UICollectionDatasource
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat itemWidth = (CGFloat)(KScreenWidth - 15 * 2 - 5 * 3) / (CGFloat)4;
return CGSizeMake(itemWidth, 105);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.weekStarGiftList.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPGiftItemCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPGiftItemCollectionViewCell class]) forIndexPath:indexPath];
GiftInfoModel * giftInfo;
giftInfo= [self.weekStarGiftList objectAtIndex:indexPath.item];
if (giftInfo.giftId == self.selectGiftInfo.giftId) {
giftInfo.isSelected = YES;
} else {
giftInfo.isSelected = NO;
}
cell.giftInfo = giftInfo;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (self.weekStarGiftList.count > 0) {
GiftInfoModel * giftInfo= [self.weekStarGiftList objectAtIndex:indexPath.item];
self.selectGiftInfo = giftInfo;
[self.collectionView reloadData];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPGiftWeekStarCollectionViewCell:didSelectGift:)]) {
[self.delegate xPGiftWeekStarCollectionViewCell:self didSelectGift:giftInfo];
}
}
}
#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 - Getters And Setters
- (void)setWeekStarGiftList:(NSArray *)weekStarGiftList {
_weekStarGiftList = weekStarGiftList;
if (_weekStarGiftList.count > 0) {
self.selectGiftInfo = _weekStarGiftList.firstObject;
}
NSInteger page = 0;
if (_weekStarGiftList.count % 8 == 0) { //刚好满页
page = _weekStarGiftList.count / 8;
} else {
page = _weekStarGiftList.count / 8 + 1;
}
self.pageController.hidden = page <= 1;
[self.pageController setNumberOfPages:page];
self.pageController.currentPage = 0;
[self.collectionView reloadData];
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
XPGiftCollectionViewFlowLayout *layout = [[XPGiftCollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 5;
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.pagingEnabled = YES;
_collectionView.showsHorizontalScrollIndicator = NO;
[_collectionView registerClass:[XPGiftItemCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPGiftItemCollectionViewCell class])];
}
return _collectionView;
}
- (UIPageControl *)pageController {
if (!_pageController) {
_pageController = [[UIPageControl alloc] init];
_pageController.currentPageIndicatorTintColor = [ThemeColor giftPageIndicatorColor];
}
return _pageController;
}
@end