Files
peko-ios/YuMi/Modules/YMRoom/View/MenuContainerView/MSRoomMenuGameView.m

197 lines
7.8 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) UIVisualEffectView *blurEffectView;
@property(nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic, assign) CGFloat itemHeight;
@property (nonatomic, assign) CGFloat emptyHeight;
@end
@implementation MSRoomMenuGameView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
self.itemHeight = kGetScaleWidth(120);
self.emptyHeight = kGetScaleWidth(100);
self.dataSource = @[].mutableCopy;
[self installUI];
[self installConstraints];
}
return self;
}
-(void)installUI{
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.backBtn];
[self addSubview:self.ms_bgView];
[self addSubview:self.blurEffectView];
[self addSubview:self.collectionView];
}
-(void)installConstraints{
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.blurEffectView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.equalTo(self).inset(0);
make.bottom.equalTo(self).offset(12);
make.height.mas_equalTo(KScreenHeight*2/3);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self);
make.leading.trailing.equalTo(self).inset(16);
// make.height.mas_equalTo(kGetScaleWidth(246));
make.height.mas_equalTo(KScreenHeight*2/3 - 30);
}];
}
-(void)backBtnAction{
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickBackBtnAction)]){
[self.delegate didClickBackBtnAction];
}
}
-(void)setPlayList:(NSMutableArray *)playList {
_playList = playList;
[self.dataSource addObjectsFromArray:playList];
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.dataSource addObjectsFromArray:littleGameList];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
- (void)updateDataSource:(NSArray *)playList gameList:(NSArray<LittleGameInfoModel *> *)littleGameList {
_playList = playList.mutableCopy;
_littleGameList = littleGameList.mutableCopy;
self.dataSource = [NSMutableArray array];
[self.dataSource addObjectsFromArray:playList];
[self.dataSource addObjectsFromArray:littleGameList];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
- (NSInteger)countOfCurrentType {
return self.dataSource.count;// 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 - 64)/4;
return [self countOfCurrentType] > 0 ? CGSizeMake(width, self.itemHeight) : CGSizeMake(KScreenWidth, self.emptyHeight);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return [self countOfCurrentType] > 0 ? UIEdgeInsetsMake(0, kGetScaleWidth(3), 0, kGetScaleWidth(3)) : UIEdgeInsetsZero;
}
- (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];
id model = [self.dataSource xpSafeObjectAtIndex:indexPath.row];
if ([model isKindOfClass:[XPRoomMoreItemModel class]]) {
cell.moreItemModel = model;
} else if ([model isKindOfClass:[ActivityInfoModel class]]) {
cell.model = model;
} else {
cell.littleGameModel = model;
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.dataSource.count > 0 && self.delegate) {
id model = [self.dataSource xpSafeObjectAtIndex:indexPath.row];
if ([model isKindOfClass:[LittleGameInfoModel class]]) {
if ([self.delegate respondsToSelector:@selector(ms_didSelectLittleGameItemAtIndexPath:)]) {
[self.delegate ms_didSelectLittleGameItemAtIndexPath:model];
}
} else {
if ([self.delegate respondsToSelector:@selector(ms_didSelectItemAtIndexPath:)]) {
[self.delegate ms_didSelectItemAtIndexPath:model];
}
}
}
}
#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;
}
return _ms_bgView;
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 8;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = 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;
}
- (UIVisualEffectView *)blurEffectView {
if (!_blurEffectView) {
// 创建模糊效果
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
// 创建包含模糊效果的视图
_blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
// 设置模糊视图的大小与目标视图一致
_blurEffectView.frame = CGRectMake(0, 0, KScreenWidth, kGetScaleWidth(246));
_blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_blurEffectView setCornerRadius:24];
}
return _blurEffectView;
}
@end