Files
peko-ios/YuMi/Modules/YMNewHome/View/CustomView/XPHomeGameView.m
2024-04-12 15:55:09 +08:00

159 lines
6.3 KiB
Objective-C

//
// XPHomeGameView.m
// xplan-ios
//
// Created by duoban on 2022/11/21.
//
#import "XPHomeGameView.h"
#import "XPHomeGameCell.h"
@interface XPHomeGameView()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic,strong) UILabel *titleView;
@property (nonatomic,strong) UIButton *backView;
@property (nonatomic,strong) UIButton *confirmBtn;
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSIndexPath *path;
@property (nonatomic,strong) UIView *bgView;
@end
@implementation XPHomeGameView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[self addSubview:self.bgView];
[self.bgView addSubview:self.titleView];
[self.bgView addSubview:self.backView];
[self.bgView addSubview:self.collectionView];
[self.bgView addSubview:self.confirmBtn];
}
- (void)initSubViewConstraints {
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.trailing.leading.equalTo(self);
make.height.mas_equalTo(kGetScaleWidth(362));
}];
[self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kGetScaleWidth(14));
make.leading.mas_equalTo(kGetScaleWidth(15));
make.height.mas_equalTo(kGetScaleWidth(22));
}];
[self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(kGetScaleWidth(26));
make.trailing.mas_equalTo(-kGetScaleWidth(15));
make.centerY.equalTo(self.titleView);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.leading.mas_equalTo(self.bgView);
make.height.mas_equalTo(kGetScaleWidth(118));
make.top.mas_equalTo(kGetScaleWidth(74));
}];
[self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.equalTo(self.bgView).inset(kGetScaleWidth(15));
make.height.mas_equalTo(kGetScaleWidth(46));
make.top.equalTo(self.collectionView.mas_bottom).mas_offset(kGetScaleWidth(70));
}];
}
- (void)setPlayGameList:(NSArray *)playGameList{
_playGameList = playGameList;
[_collectionView reloadData];
}
#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.playGameList.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPHomeGameCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeGameCell class]) forIndexPath:indexPath];
LittleGameInfoModel *model = self.playGameList[indexPath.row];
cell.imageUrl = model.pic;
if(self.path != nil){
cell.isChoose = self.path.row == indexPath.row;
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.confirmBtn.enabled = YES;
self.path = indexPath;
[self.collectionView reloadData];
}
-(void)confirmAction{
[self removeFromSuperview];
LittleGameInfoModel *model = self.playGameList[self.path.row];
if(self.delegate && [self.delegate respondsToSelector:@selector(xpHomeGameViewChooseGameWithGameModel:)]){
[self.delegate xpHomeGameViewChooseGameWithGameModel:model];
}
}
-(void)backAction{
[self removeFromSuperview];
}
#pragma mark -懒加载
- (UIView *)bgView{
if (!_bgView){
_bgView = [UIView new];
_bgView.backgroundColor = [UIColor whiteColor];
[_bgView setCornerWithLeftTopCorner:kGetScaleWidth(25) rightTopCorner:kGetScaleWidth(25) bottomLeftCorner:0 bottomRightCorner:0 size:CGSizeMake(KScreenWidth, kGetScaleWidth(362))];
}
return _bgView;
}
- (UILabel *)titleView{
if (!_titleView){
_titleView = [UILabel labelInitWithText:YMLocalizedString(@"XPHomeGameView0") font:kFontMedium(kGetScaleWidth(16)) textColor:[DJDKMIMOMColor inputTextColor]];
}
return _titleView;
}
-(UIButton *)backView{
if (!_backView){
_backView = [UIButton buttonInitWithText:nil font:nil textColor:nil image:kImage(@"home_game_back") bgImage:nil];
[_backView addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
}
return _backView;
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout alloc] init];
layout.itemSize = CGSizeMake(kGetScaleWidth(98), kGetScaleWidth(118));
layout.sectionInset = UIEdgeInsetsMake(0, kGetScaleWidth(15), 0, kGetScaleWidth(15));
layout.minimumLineSpacing = kGetScaleWidth(15);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[XPHomeGameCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeGameCell class])];
}
return _collectionView;
}
- (UIButton *)confirmBtn{
if (!_confirmBtn){
UIImage *image = [UIImage gradientColorImageFromColors:@[[DJDKMIMOMColor confirmButtonGradientStartColor],[DJDKMIMOMColor confirmButtonGradientMiddleColor],[DJDKMIMOMColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(KScreenWidth - kGetScaleWidth(30), kGetScaleWidth(46))];
_confirmBtn = [UIButton buttonInitWithText:YMLocalizedString(@"XPHomeGameView1") font:kFontRegular(16) textColor:[UIColor whiteColor] image:nil bgImage:image];
_confirmBtn.layer.cornerRadius = kGetScaleWidth(23);
_confirmBtn.layer.masksToBounds = YES;
[_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
_confirmBtn.enabled = NO;
}
return _confirmBtn;
}
@end