191 lines
7.8 KiB
Objective-C
191 lines
7.8 KiB
Objective-C
//
|
|
// MewUnlockGiftBroadcastView.m
|
|
// xplan-ios
|
|
//
|
|
// Created by duoban on 2024/1/26.
|
|
//
|
|
|
|
#import "MewUnlockGiftBroadcastView.h"
|
|
#import "MewUnlockGiftBroadcastViewCell.h"
|
|
@interface MewUnlockGiftBroadcastView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,MewUnlockGiftBroadcastViewCellDelegate>
|
|
@property(nonatomic,strong) UICollectionView *collectionView;
|
|
@property(nonatomic,strong) NetImageView *dressUpView;
|
|
@end
|
|
@implementation MewUnlockGiftBroadcastView
|
|
|
|
-(instancetype)initWithFrame:(CGRect)frame{
|
|
self = [super initWithFrame:frame];
|
|
if(self){
|
|
[self installUI];
|
|
[self installConstraints];
|
|
}
|
|
return self;
|
|
}
|
|
-(void)installUI{
|
|
self.backgroundColor = UIColorRGBAlpha(0x0A0A0A , 1);
|
|
self.layer.cornerRadius = 10;
|
|
self.layer.masksToBounds = YES;
|
|
[self addSubview:self.collectionView];
|
|
[self addSubview:self.dressUpView];
|
|
}
|
|
-(void)installConstraints{
|
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self);
|
|
}];
|
|
[self.dressUpView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.edges.equalTo(self);
|
|
}];
|
|
}
|
|
-(void)setGiftModel:(GiftInfoModel *)giftModel{
|
|
_giftModel = giftModel;
|
|
self.dressUpView.image = nil;
|
|
if (_giftModel.bannerUrl.length > 0){
|
|
[self.dressUpView loadImageWithUrl:_giftModel.bannerUrl completion:^(UIImage * _Nonnull image, NSURL * _Nonnull url) {
|
|
self.dressUpView.image = image;
|
|
}];
|
|
}
|
|
[self.collectionView reloadData];
|
|
if(_giftModel.baseGift.isSelect == YES){
|
|
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
|
|
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
|
|
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
|
|
});
|
|
}else{
|
|
|
|
int j = 0;
|
|
for (int i = 0; i < _giftModel.unlockGiftList.count ; i++) {
|
|
MewUnlockGiftModel *model = self.giftModel.unlockGiftList[i];
|
|
if(model.isSelect){
|
|
j = i;
|
|
break;
|
|
}
|
|
|
|
}
|
|
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
|
|
dispatch_after(delayTime, dispatch_get_main_queue(), ^{
|
|
|
|
if (self.giftModel.unlockGiftList.count > j){
|
|
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:j + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
|
|
}
|
|
|
|
|
|
});
|
|
}
|
|
|
|
}
|
|
-(void)setIsDressUpGift:(BOOL)isDressUpGift{
|
|
_isDressUpGift = isDressUpGift;
|
|
_dressUpView.hidden = !_isDressUpGift;
|
|
}
|
|
#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
|
return self.giftModel.unlockGiftList.count + 1;
|
|
}
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
if (indexPath.row == 0){
|
|
return CGSizeMake(self.giftModel.baseGift.width, 32);
|
|
}
|
|
MewUnlockGiftModel *model = self.giftModel.unlockGiftList[indexPath.row - 1];
|
|
return CGSizeMake(model.width, 32);
|
|
|
|
}
|
|
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
MewUnlockGiftBroadcastViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MewUnlockGiftBroadcastViewCell class]) forIndexPath:indexPath];
|
|
cell.isBaseGift = indexPath.row == 0;
|
|
cell.baseModel = self.giftModel.baseGift;
|
|
if (indexPath.row > 0){
|
|
cell.unlockModel = [self.giftModel.unlockGiftList safeObjectAtIndex1:indexPath.row - 1];
|
|
}
|
|
cell.path = indexPath;
|
|
cell.delegate = self;
|
|
|
|
return cell;
|
|
}
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
|
|
|
for (int i = 0 ; i < self.giftModel.unlockGiftList.count; i++) {
|
|
MewUnlockGiftModel *model = self.giftModel.unlockGiftList[i];
|
|
if (indexPath.row == 0){
|
|
model.isSelect = NO;
|
|
self.giftModel.baseGift.isSelect = YES;
|
|
}else{
|
|
model.isSelect = i == indexPath.row-1;
|
|
self.giftModel.baseGift.isSelect = NO;
|
|
}
|
|
|
|
}
|
|
if (indexPath.row == 0){
|
|
MewUnlockGiftModel *model = [MewUnlockGiftModel new];
|
|
model.targetGift = self.giftModel.baseGift;
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(mewUnlockGiftBroadcastViewDidChooseUnlockGift:)]){
|
|
[self.delegate mewUnlockGiftBroadcastViewDidChooseUnlockGift:model];
|
|
}
|
|
}else{
|
|
if (self.delegate && [self.delegate respondsToSelector:@selector(mewUnlockGiftBroadcastViewDidChooseUnlockGift:)]){
|
|
[self.delegate mewUnlockGiftBroadcastViewDidChooseUnlockGift:self.giftModel.unlockGiftList[indexPath.row-1]];
|
|
}
|
|
}
|
|
|
|
[self.collectionView reloadData];
|
|
}
|
|
-(void)updateGiftInfo:(GiftInfoModel *)giftModel count:(int)count{
|
|
// MewUnlockGiftModel *model1 = self.giftModel.unlockGiftList[2];
|
|
// if (giftModel.giftId == self.giftModel.baseGift.giftId){
|
|
// for (int i = 0 ; i < self.giftModel.unlockGiftList.count; i++) {
|
|
// MewUnlockGiftModel *model = self.giftModel.unlockGiftList[i];
|
|
// int num = model.condition - model.process;
|
|
// if (count > num){
|
|
// model.process = model.condition;
|
|
// count = count - num;
|
|
// }else{
|
|
// model.process = model.process + count;
|
|
// }
|
|
//
|
|
// }
|
|
// }
|
|
// MewUnlockGiftModel *model2 = self.giftModel.unlockGiftList[2];
|
|
[self.collectionView reloadData];
|
|
}
|
|
#pragma mark - MewUnlockGiftBroadcastViewCellDelegate
|
|
- (void)didSelectItemAtIndexPath:(NSIndexPath *)path{
|
|
[self.collectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
|
|
|
|
}
|
|
-(void)clickDressUpGiftAction{
|
|
if(self.delegate && [self.delegate respondsToSelector:@selector(mewUnlockGiftBroadcastViewDidChooseDressUpGift:)]){
|
|
[self.delegate mewUnlockGiftBroadcastViewDidChooseDressUpGift:self.giftModel];
|
|
}
|
|
}
|
|
#pragma mark - 懒加载
|
|
- (UICollectionView *)collectionView{
|
|
if(!_collectionView){
|
|
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
|
|
layout.minimumLineSpacing = 0;
|
|
layout.minimumInteritemSpacing = 4;
|
|
layout.sectionInset = UIEdgeInsetsMake(0, 6, 0, 6);
|
|
|
|
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
|
|
|
|
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
|
|
_collectionView.delegate = self;
|
|
_collectionView.dataSource = self;
|
|
_collectionView.showsVerticalScrollIndicator = NO;
|
|
_collectionView.showsHorizontalScrollIndicator = NO;
|
|
_collectionView.backgroundColor = [UIColor clearColor];
|
|
[_collectionView registerClass:[MewUnlockGiftBroadcastViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MewUnlockGiftBroadcastViewCell class])];
|
|
}
|
|
return _collectionView;
|
|
}
|
|
- (NetImageView *)dressUpView{
|
|
if(!_dressUpView){
|
|
|
|
_dressUpView = [[NetImageView alloc]init];
|
|
_dressUpView.hidden = YES;
|
|
_dressUpView.userInteractionEnabled = YES;
|
|
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickDressUpGiftAction)];
|
|
[_dressUpView addGestureRecognizer:tap];
|
|
}
|
|
return _dressUpView;
|
|
}
|
|
@end
|