Files
peko-ios/YuMi/Modules/YMMine/View/Cell/XPMineGameTableViewCell.m
2024-04-12 15:55:09 +08:00

198 lines
7.4 KiB
Objective-C

//
// XPMineGameTableViewCell.m
// xplan-ios
//
// Created by GreenLand on 2022/7/22.
//
#import "XPMineGameTableViewCell.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "UIImage+Utils.h"
///View
#import "XPMIneGameCollectionViewCell.h"
@interface XPMineGameTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIImageView *titleBgView;
///列表
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *slideBackView;
@property (nonatomic, strong) UIView *sliderView;
@end
@implementation XPMineGameTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:self.mainView];
[self.mainView addSubview:self.titleBgView];
[self.mainView addSubview:self.titleLabel];
[self.mainView addSubview:self.collectionView];
}
- (void)initSubViewConstraints {
[self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.mas_equalTo(0);
make.leading.trailing.mas_equalTo(self.contentView).inset(15);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.mas_equalTo(12);
make.height.mas_equalTo(18);
}];
[self.titleBgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(self.titleLabel);
make.bottom.mas_equalTo(self.titleLabel).mas_offset(1);
make.size.mas_equalTo(CGSizeMake(41, 10));
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(14);
make.leading.trailing.mas_equalTo(0);
make.height.mas_equalTo(63);
}];
}
#pragma mark - UICollectionViewDatasource And UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.datasource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPMIneGameCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPMIneGameCollectionViewCell class]) forIndexPath:indexPath];
LittleGameInfoModel * item = [self.datasource safeObjectAtIndex1:indexPath.row];
cell.gameModel = item;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if (self.delegate && [self.delegate respondsToSelector:@selector(xPMineGameTableViewCell:didSelectItem:)]) {
LittleGameInfoModel * model = [self.datasource safeObjectAtIndex1:indexPath.row];
[self.delegate xPMineGameTableViewCell:self didSelectItem:model];
}
}
#pragma mark - MSBaseRTLFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = (KScreenWidth - 30) / 4;
return CGSizeMake(width, 63);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint offset = scrollView.contentOffset;
CGRect frame = self.sliderView.frame;
frame.origin.x = offset.x*12/(scrollView.contentSize.width-scrollView.frame.size.width);
self.sliderView.frame = frame;
}
#pragma mark - Getters And Setters
- (void)setDatasource:(NSArray<LittleGameInfoModel *> *)datasource {
_datasource = datasource;
if(datasource.count > 4) {
[self.mainView addSubview:self.slideBackView];
[self.slideBackView addSubview:self.sliderView];
[self.slideBackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mainView);
make.size.mas_equalTo(CGSizeMake(24, 4));
make.top.mas_equalTo(self.collectionView.mas_bottom).mas_offset(10);
}];
[self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(12, 4));
make.top.leading.bottom.mas_equalTo(self.slideBackView);
}];
} else {
[self.sliderView removeFromSuperview];
[self.slideBackView removeFromSuperview];
}
[self.collectionView reloadData];
}
- (UIView *)mainView {
if (!_mainView) {
_mainView = [[UIView alloc] init];
_mainView.backgroundColor = [UIColor whiteColor];
_mainView.layer.cornerRadius = 8;
_mainView.layer.masksToBounds = YES;
}
return _mainView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = YMLocalizedString(@"XPMineGameTableViewCell0");
_titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
_titleLabel.textColor = [DJDKMIMOMColor mainTextColor];
}
return _titleLabel;
}
- (UIImageView *)titleBgView {
if (!_titleBgView) {
_titleBgView = [[UIImageView alloc] init];
_titleBgView.image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0xFFD15A), UIColorFromRGB(0xFFC000)] gradientType:GradientTypeTopToBottom imgSize:CGSizeMake(41, 10)];
_titleBgView.layer.cornerRadius = 5;
_titleBgView.layer.masksToBounds = YES;
}
return _titleBgView;
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [DJDKMIMOMColor appCellBackgroundColor];
[_collectionView registerClass:[XPMIneGameCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPMIneGameCollectionViewCell class])];
}
return _collectionView;
}
- (UIView *)sliderView {
if (!_sliderView) {
_sliderView = [[UIView alloc] init];
_sliderView.backgroundColor = UIColorFromRGB(0xFFBC51);
_sliderView.layer.cornerRadius = 2;
_sliderView.layer.masksToBounds = YES;
}
return _sliderView;
}
- (UIView *)slideBackView {
if (!_slideBackView) {
_slideBackView = [[UIView alloc] init];
_slideBackView.backgroundColor = UIColorRGBAlpha(0x000000, 0.1);
_slideBackView.layer.cornerRadius = 2;
_slideBackView.layer.masksToBounds = YES;
_sliderView.clipsToBounds = YES;
}
return _slideBackView;
}
@end