185 lines
7.5 KiB
Objective-C
185 lines
7.5 KiB
Objective-C
//
|
|
// MSRoomMenuGameView.m
|
|
// YuMi
|
|
//
|
|
// Created by duoban on 2024/4/29.
|
|
//
|
|
|
|
#import "MSRoomMenuGameView.h"
|
|
#import "MSRoomMenuGameCell.h"
|
|
#import "XNDJTDDLoadingTool.h"
|
|
#import "MSRoomMenuGameEmptyCell.h"
|
|
|
|
static const NSInteger kItemsPerRow = 5;
|
|
|
|
@interface MSRoomMenuGameView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
|
|
@property(nonatomic,strong) UIButton *backBtn;
|
|
@property(nonatomic,strong) UIView *ms_bgView;
|
|
@property(nonatomic,strong) UICollectionView *collectionView;
|
|
|
|
@property (nonatomic, assign) CGFloat itemHeight;
|
|
|
|
@end
|
|
@implementation MSRoomMenuGameView
|
|
|
|
-(instancetype)initWithFrame:(CGRect)frame{
|
|
self = [super initWithFrame:frame];
|
|
if(self){
|
|
|
|
self.itemHeight = kGetScaleWidth(80);
|
|
|
|
[self installUI];
|
|
[self installConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
-(void)installUI{
|
|
self.backgroundColor = [UIColor clearColor];
|
|
[self addSubview:self.backBtn];
|
|
[self addSubview:self.ms_bgView];
|
|
[self addSubview:self.collectionView];
|
|
}
|
|
-(void)installConstraints{
|
|
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self);
|
|
}];
|
|
[self.ms_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.equalTo(self).inset(0);
|
|
make.bottom.equalTo(self).offset(12);
|
|
make.height.mas_equalTo(kGetScaleWidth(246));
|
|
}];
|
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.leading.trailing.equalTo(self).inset(kGetScaleWidth(0));
|
|
make.height.mas_equalTo(kGetScaleWidth(246));
|
|
}];
|
|
}
|
|
-(void)backBtnAction{
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickBackBtnAction)]){
|
|
[self.delegate didClickBackBtnAction];
|
|
}
|
|
}
|
|
-(void)setPlayList:(NSMutableArray *)playList {
|
|
_playList = playList;
|
|
[self updateViewHeightWithItemCount:_playList.count];
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
[self.collectionView reloadData];
|
|
});
|
|
}
|
|
|
|
- (void)setLittleGameList:(NSMutableArray<LittleGameInfoModel *> *)littleGameList {
|
|
_littleGameList = littleGameList;
|
|
[self updateViewHeightWithItemCount:_littleGameList.count];
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
[self.collectionView reloadData];
|
|
});
|
|
}
|
|
|
|
- (void)updateViewHeightWithItemCount:(NSInteger)count {
|
|
NSInteger lineNum = MIN(3, (count + kItemsPerRow - 1)/kItemsPerRow);
|
|
|
|
if (lineNum == 1) {
|
|
self.itemHeight = kGetScaleWidth(100);
|
|
}
|
|
|
|
CGFloat calHeight = kGetScaleWidth(80);
|
|
if (lineNum < 3) {
|
|
calHeight = kGetScaleWidth(100);
|
|
}
|
|
|
|
CGFloat height = 20 + 56 * lineNum + 44 * (lineNum - 1) + 70;
|
|
height = calHeight * lineNum;
|
|
// [_ms_bgView setCornerWithLeftTopCorner:kGetScaleWidth(12) rightTopCorner:kGetScaleWidth(12) bottomLeftCorner:0 bottomRightCorner:0 size:CGSizeMake(KScreenWidth, height-48)];
|
|
[self.ms_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(kGetScaleWidth(height + 12));
|
|
}];
|
|
[self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
make.height.mas_equalTo(kGetScaleWidth(height));
|
|
}];
|
|
}
|
|
|
|
- (NSInteger)countOfCurrentType {
|
|
return self.littleGameList.count > 0 ? self.littleGameList.count : self.playList.count;
|
|
}
|
|
|
|
#pragma mark- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
CGFloat width = (KScreenWidth-kGetScaleWidth(6))/5;
|
|
return [self countOfCurrentType] > 0 ? CGSizeMake(width, self.itemHeight): CGSizeMake(KScreenWidth, kGetScaleWidth(200));
|
|
}
|
|
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
|
|
return [self countOfCurrentType] > 0 ? UIEdgeInsetsMake(0, kGetScaleWidth(3), 0, kGetScaleWidth(3)): UIEdgeInsetsMake(0, 0, 0, 0);
|
|
}
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
|
return [self countOfCurrentType] > 0 ? [self countOfCurrentType] : 1;
|
|
}
|
|
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
if ([self countOfCurrentType] == 0){
|
|
MSRoomMenuGameEmptyCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MSRoomMenuGameEmptyCell class]) forIndexPath:indexPath];
|
|
return cell;
|
|
}
|
|
|
|
MSRoomMenuGameCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MSRoomMenuGameCell class]) forIndexPath:indexPath];
|
|
if (_littleGameList.count > 0){
|
|
cell.littleGameModel = [self.littleGameList xpSafeObjectAtIndex:indexPath.row];
|
|
} else {
|
|
id model = [self.playList xpSafeObjectAtIndex:indexPath.row];
|
|
if ([model isKindOfClass:[XPRoomMoreItemModel class]]) {
|
|
cell.moreItemModel = model;
|
|
} else if ([model isKindOfClass:[ActivityInfoModel class]]) {
|
|
cell.model = model;
|
|
}
|
|
}
|
|
|
|
return cell;
|
|
}
|
|
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
if (self.delegate) {
|
|
if (self.littleGameList != nil &&
|
|
self.littleGameList.count > 0 &&
|
|
[self.delegate respondsToSelector:@selector(ms_didSelectLittleGameItemAtIndexPath:)]) {
|
|
[self.delegate ms_didSelectLittleGameItemAtIndexPath:[self.littleGameList xpSafeObjectAtIndex:indexPath.row]];
|
|
}
|
|
else if (self.playList != nil &&
|
|
self.playList.count > 0 &&
|
|
[self.delegate respondsToSelector:@selector(ms_didSelectItemAtIndexPath:)]) {
|
|
[self.delegate ms_didSelectItemAtIndexPath:[self.playList xpSafeObjectAtIndex:indexPath.row]];
|
|
}
|
|
}
|
|
}
|
|
#pragma mark - 懒加载
|
|
- (UIButton *)backBtn{
|
|
if(!_backBtn){
|
|
_backBtn = [UIButton new];
|
|
[_backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _backBtn;
|
|
}
|
|
- (UIView *)ms_bgView{
|
|
if(!_ms_bgView){
|
|
_ms_bgView = [UIView new];
|
|
_ms_bgView.backgroundColor = UIColorFromRGB(0x2D1E4D);
|
|
_ms_bgView.layer.cornerRadius = 12;
|
|
_ms_bgView.layer.masksToBounds = YES;
|
|
// [_ms_bgView setCornerWithLeftTopCorner:kGetScaleWidth(12) rightTopCorner:kGetScaleWidth(12) bottomLeftCorner:0 bottomRightCorner:0 size:CGSizeMake(KScreenWidth, kGetScaleWidth(247))];
|
|
}
|
|
return _ms_bgView;
|
|
}
|
|
- (UICollectionView *)collectionView{
|
|
if (!_collectionView) {
|
|
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
|
|
|
|
layout.minimumLineSpacing = 0;
|
|
layout.minimumInteritemSpacing = 0;
|
|
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
|
|
_collectionView.showsHorizontalScrollIndicator = NO;
|
|
_collectionView.dataSource = self;
|
|
_collectionView.delegate = self;
|
|
_collectionView.backgroundColor = [UIColor clearColor];
|
|
[_collectionView registerClass:[MSRoomMenuGameCell class] forCellWithReuseIdentifier:NSStringFromClass([MSRoomMenuGameCell class])];
|
|
[_collectionView registerClass:[MSRoomMenuGameEmptyCell class] forCellWithReuseIdentifier:NSStringFromClass([MSRoomMenuGameEmptyCell class])];
|
|
|
|
}
|
|
return _collectionView;
|
|
}
|
|
@end
|