// // QXMineAddSongGiftView.m // QXLive // // Created by 启星 on 2025/11/13. // #import "QXMineAddSongGiftView.h" #import "QXGiftCell.h" #import "QXMineNetwork.h" #import "QXMenuPopView.h" @interface QXMineAddSongGiftView() @property (nonatomic,strong)UIView *bgView; @property (nonatomic,strong)UIImageView *bgImageView; @property (nonatomic,strong)UICollectionView *collectionView; @property (nonatomic,strong)NSMutableArray *dataArray; @property (nonatomic,assign)NSInteger selectedIndex; @property (nonatomic,strong)QXGiftModel *model; @property (nonatomic,strong)UIView *sendBgView; @property (nonatomic,strong)UIButton *countBtn; @property (nonatomic,strong)UIButton *sendBtn; @property (nonatomic,strong)UIButton *sendAllBtn; @property (nonatomic,strong)NSString* giftCount; @end @implementation QXMineAddSongGiftView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubviews]; } return self; } -(void)initSubviews{ self.giftCount = @"1"; self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)]; tap.delegate = self; [self addGestureRecognizer:tap]; self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScaleWidth(429)+kSafeAreaBottom)]; [self.bgView addRoundedCornersWithRadius:16 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)]; [self addSubview:self.bgView]; self.bgImageView = [[UIImageView alloc] initWithFrame:self.bounds]; self.bgImageView.image = [UIImage imageNamed:@"room_sound_bg"]; [self.bgView addSubview:self.bgImageView]; self.selectedIndex = -1; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake((SCREEN_WIDTH-16*2-12*3)/4, ScaleWidth(119)); layout.minimumLineSpacing = 12; layout.minimumInteritemSpacing = 12; layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16); layout.scrollDirection = UICollectionViewScrollDirectionVertical; self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 16, self.width, self.bgView.height-12) collectionViewLayout:layout]; [self.collectionView registerNib:[UINib nibWithNibName:@"QXGiftCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QXGiftCell"]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.bounces = NO; self.collectionView.backgroundColor = [UIColor clearColor]; [self.bgView addSubview:self.collectionView]; self.sendBgView = [[UIView alloc] init]; [self.sendBgView addRoundedCornersWithRadius:17.5]; self.sendBgView.layer.borderColor = QXConfig.themeColor.CGColor; self.sendBgView.layer.borderWidth = 1; [self.bgView addSubview:self.sendBgView]; [self.sendBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-16); make.width.mas_equalTo(120); make.height.mas_equalTo(35); make.bottom.mas_equalTo(-kSafeAreaBottom-12); }]; self.countBtn = [[UIButton alloc] init]; [self.countBtn setTitle:@"X1" forState:(UIControlStateNormal)]; [self.countBtn setTitleColor:RGB16(0xffffff) forState:(UIControlStateNormal)]; self.countBtn.titleLabel.font = [UIFont systemFontOfSize:12]; [self.countBtn addTarget:self action:@selector(selectedCount:) forControlEvents:(UIControlEventTouchUpInside)]; [self.sendBgView addSubview:self.countBtn]; [self.countBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.bottom.equalTo(self.sendBgView); make.width.equalTo(self.sendBgView.mas_width).multipliedBy(0.5); }]; self.sendBtn = [[UIButton alloc] init]; [self.sendBtn setTitle:QXText(@"确定") forState:(UIControlStateNormal)]; [self.sendBtn setTitleColor:QXConfig.btnTextColor forState:(UIControlStateNormal)]; self.sendBtn.backgroundColor = QXConfig.themeColor; self.sendBtn.titleLabel.font = [UIFont systemFontOfSize:12]; [self.sendBtn addTarget:self action:@selector(sendAction) forControlEvents:(UIControlEventTouchUpInside)]; [self.sendBgView addSubview:self.sendBtn]; [self.sendBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.top.bottom.equalTo(self.sendBgView); make.width.equalTo(self.sendBgView.mas_width).multipliedBy(0.5); }]; [self getGiftList]; } -(void)didSelectedIndex:(NSInteger)index menuTitle:(NSString *)menuTitle{ [self.countBtn setTitle:menuTitle forState:(UIControlStateNormal)]; NSCharacterSet* nonDigits =[[NSCharacterSet decimalDigitCharacterSet] invertedSet]; NSInteger count =[[menuTitle stringByTrimmingCharactersInSet:nonDigits] integerValue]; QXLOG(@"选择了%ld个",count); self.giftCount = [NSString stringWithFormat:@"%ld",count]; } -(void)selectedCount:(UIButton*)sender{ QXMenuPopView *popView = [[QXMenuPopView alloc] initWithPoint:CGPointMake(SCREEN_WIDTH-16-60-30, SCREEN_HEIGHT-kSafeAreaBottom-50-225) width:88 height:225]; popView.type = QXMenuPopViewTypeArrowBottom; popView.dataArray = @[@"X20",@"X15",@"X10",@"X5",@"X1"]; popView.delegate = self; [popView showInView:KEYWINDOW]; } -(void)sendAction{ if (self.model == nil) { showToast(@"请选择礼物"); return; } if (self.delegate && [self.delegate respondsToSelector:@selector(didSeletedGift:giftCount:)]) { [self.delegate didSeletedGift:self.model giftCount:self.giftCount]; } [self hide]; } -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return touch.view == self; } -(void)showInView:(UIView *)view{ self.bgView.y = SCREEN_HEIGHT; [view addSubview:self]; [UIView animateWithDuration:0.3 animations:^{ self.bgView.y = SCREEN_HEIGHT-ScaleWidth(429)-kSafeAreaBottom; }]; } -(void)hide{ [UIView animateWithDuration:0.3 animations:^{ self.bgView.y = SCREEN_HEIGHT; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } -(void)getGiftList{ MJWeakSelf [QXMineNetwork giftListWithLabel:@"1" roomId:@"" successBlock:^(NSArray * _Nonnull list) { [weakSelf.dataArray removeAllObjects]; [weakSelf.dataArray addObjectsFromArray:list]; [weakSelf.collectionView reloadData]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.dataArray.count; } -(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ QXGiftCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXGiftCell" forIndexPath:indexPath]; cell.cellType = QXGiftCellTypeLive; cell.roomGiftModel = self.dataArray[indexPath.row]; if (self.selectedIndex == indexPath.row) { cell.selecteBtn.selected = YES; cell.giftNameLabel.textColor = RGB16(0x333333); [cell.cornBtn setTitleColor:RGB16(0x333333) forState:(UIControlStateNormal)]; }else{ cell.selecteBtn.selected = NO; cell.giftNameLabel.textColor = RGB16(0xffffff); [cell.cornBtn setTitleColor:RGB16(0xffffff) forState:(UIControlStateNormal)]; } return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == self.selectedIndex) { return; } self.selectedIndex = indexPath.row; self.model = self.dataArray[indexPath.row]; [collectionView reloadData]; } -(NSMutableArray *)dataArray{ if (!_dataArray) { _dataArray = [NSMutableArray array] ; } return _dataArray; } @end