Files
peko-ios/YuMi/Modules/YMRoom/View/MenuContainerView/MSRoomMenuGameView.m
2024-05-16 14:30:21 +08:00

124 lines
5.2 KiB
Objective-C

//
// MSRoomMenuGameView.m
// YuMi
//
// Created by duoban on 2024/4/29.
//
#import "MSRoomMenuGameView.h"
#import "MSRoomMenuGameCell.h"
#import "MSRoomMenuGameEmptyCell.h"
@interface MSRoomMenuGameView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong) UIButton *backBtn;
@property(nonatomic,strong) UIView *ms_bgView;
@property(nonatomic,strong) UICollectionView *collectionView;
@end
@implementation MSRoomMenuGameView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self installUI];
[self installConstraints];
}
return self;
}
-(void)installUI{
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.backBtn];
[self addSubview:self.ms_bgView];
[self.ms_bgView addSubview:self.collectionView];
}
-(void)installConstraints{
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
[self.ms_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.leading.trailing.equalTo(self).inset(kGetScaleWidth(0));
make.height.mas_equalTo(kGetScaleWidth(247));
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.ms_bgView);
}];
}
-(void)backBtnAction{
if (self.delegate && [self.delegate respondsToSelector:@selector(didClickBackBtnAction)]){
[self.delegate didClickBackBtnAction];
}
}
-(void)setPlayList:(NSMutableArray<ActivityInfoModel *> *)playList{
_playList = playList;
if(_playList.count < 6){
[self.ms_bgView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kGetScaleWidth(130));
}];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
#pragma mark- UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat width = (KScreenWidth-kGetScaleWidth(6))/5;
return self.playList.count > 0 ? CGSizeMake(width, kGetScaleWidth(100)): CGSizeMake(KScreenWidth, kGetScaleWidth(200));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return self.playList.count > 0 ? UIEdgeInsetsMake(0, kGetScaleWidth(3), 0, kGetScaleWidth(3)): UIEdgeInsetsMake(0, 0, 0, 0);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.playList.count > 0 ? self.playList.count : 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.playList.count == 0){
MSRoomMenuGameEmptyCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MSRoomMenuGameEmptyCell class]) forIndexPath:indexPath];
return cell;
}
MSRoomMenuGameCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MSRoomMenuGameCell class]) forIndexPath:indexPath];
cell.model = [self.playList safeObjectAtIndex1:indexPath.row];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.delegate && [self.delegate respondsToSelector:@selector(ms_didSelectItemAtIndexPath:)]){
[self.delegate ms_didSelectItemAtIndexPath:[self.playList safeObjectAtIndex1: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 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