// // QXSoundListView.m // QXLive // // Created by 启星 on 2025/6/10. // #import "QXSoundListView.h" #import "QXAgoraEngine.h" @interface QXSoundListView() @property (nonatomic,strong)UIView *bgView; @property (nonatomic,strong)UIImageView *bgImageView; @property (nonatomic,strong)UILabel *titleLabel; @property (nonatomic,strong)UICollectionView *collectionView; @property (nonatomic,strong)UIButton *commitBtn; @property (nonatomic,strong)NSMutableArray *dataArray; @property (nonatomic,strong)QXSoundListModel *selectedModel; @end @implementation QXSoundListView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubviews]; } return self; } -(void)initSubviews{ QXSoundListModel *md = [[QXSoundListModel alloc] init]; md.name = QXText(@"笑声"); md.icon = @"room_sound_icon_1"; md.filePath = [[NSBundle mainBundle] pathForResource:@"音效-笑声" ofType:@"mp3"]; QXSoundListModel *md1 = [[QXSoundListModel alloc] init]; md1.name = QXText(@"欢呼"); md1.icon = @"room_sound_icon_2"; md1.filePath = [[NSBundle mainBundle] pathForResource:@"音效-欢呼" ofType:@"mp3"]; QXSoundListModel *md2 = [[QXSoundListModel alloc] init]; md2.name = QXText(@"尴尬"); md2.icon = @"room_sound_icon_3"; md2.filePath = [[NSBundle mainBundle] pathForResource:@"音效-尴尬" ofType:@"mp3"]; QXSoundListModel *md3 = [[QXSoundListModel alloc] init]; md3.name = QXText(@"尖叫"); md3.icon = @"room_sound_icon_4"; md3.filePath = [[NSBundle mainBundle] pathForResource:@"音效-掌声" ofType:@"mp3"]; QXSoundListModel *md4 = [[QXSoundListModel alloc] init]; md4.name = QXText(@"么么哒"); md4.icon = @"room_sound_icon_5"; md4.filePath = [[NSBundle mainBundle] pathForResource:@"音效-么么哒" ofType:@"mp3"]; [self.dataArray addObject:md]; [self.dataArray addObject:md1]; [self.dataArray addObject:md2]; [self.dataArray addObject:md3]; [self.dataArray addObject:md4]; 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, SCREEN_HEIGHT, SCREEN_WIDTH, ScaleWidth(279)+kSafeAreaBottom)]; // self.bgView.backgroundColor = [UIColor whiteColor]; [self addSubview:self.bgView]; self.bgImageView = [[UIImageView alloc] initWithFrame:self.bgView.bounds]; self.bgImageView.image = [UIImage imageNamed:@"room_sound_bg"]; [self.bgView addSubview:self.bgImageView]; self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, SCREEN_WIDTH-32, 27)]; self.titleLabel.textColor = UIColor.whiteColor; self.titleLabel.font = [UIFont boldSystemFontOfSize:18]; self.titleLabel.textAlignment = NSTextAlignmentCenter; self.titleLabel.text = QXText(@"选择音效"); [self.bgView addSubview:self.titleLabel]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat itemWidth = (SCREEN_WIDTH-16*2-10*4)/5; layout.itemSize = CGSizeMake(itemWidth, 100); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 0; layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16); self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom+22, self.bgView.width, self.bgView.height-self.titleLabel.bottom-22-kSafeAreaBottom) collectionViewLayout:layout]; [self.collectionView registerClass:[QXSoundListCell class] forCellWithReuseIdentifier:@"QXSoundListCell"]; self.collectionView.delegate = self; self.collectionView.dataSource = self; self.collectionView.backgroundColor = [UIColor clearColor]; self.collectionView.showsHorizontalScrollIndicator = NO; self.collectionView.bounces = NO; [self.bgView addSubview:self.collectionView]; self.commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(38, self.bgView.height-kSafeAreaBottom-42, SCREEN_WIDTH-38*2, 42)]; [self.commitBtn setTitle:QXText(@"确定") forState:(UIControlStateNormal)]; [self.commitBtn setTitleColor:QXConfig.btnTextColor forState:(UIControlStateNormal)]; [self.commitBtn addTarget:self action:@selector(commitAction) forControlEvents:(UIControlEventTouchUpInside)]; [self.commitBtn addRoundedCornersWithRadius:21]; self.commitBtn.titleLabel.font = [UIFont systemFontOfSize:14]; self.commitBtn.backgroundColor = QXConfig.themeColor; [self.bgView addSubview:self.commitBtn]; } -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return touch.view == self; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.dataArray.count; } -(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ QXSoundListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXSoundListCell" forIndexPath:indexPath]; cell.model = self.dataArray[indexPath.row]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ if (self.selectedModel == nil) { QXSoundListModel *model = self.dataArray[indexPath.row]; model.isSelected = YES; self.selectedModel = model; [collectionView reloadData]; }else{ QXSoundListModel *model = self.dataArray[indexPath.row]; if (model.isSelected) { return; } self.selectedModel.isSelected = NO; model.isSelected = YES; self.selectedModel = model; [collectionView reloadData]; } } -(void)showInView:(UIView *)view{ [view addSubview:self]; [UIView animateWithDuration:0.3 animations:^{ self.bgView.y = SCREEN_HEIGHT-ScaleWidth(279)-kSafeAreaBottom; }]; } -(void)hide{ [UIView animateWithDuration:0.3 animations:^{ self.bgView.y = SCREEN_HEIGHT; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } -(void)commitAction{ [self hide]; [[QXAgoraEngine sharedEngine].agoraKit playEffect:1 filePath:self.selectedModel.filePath loopCount:0 pitch:1 pan:0 gain:100 publish:YES]; } -(NSMutableArray *)dataArray{ if (!_dataArray) { _dataArray = [NSMutableArray array]; } return _dataArray; } @end @implementation QXSoundListCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubviews]; } return self; } - (void)setModel:(QXSoundListModel *)model{ _model = model; self.titleBtn.selected = model.isSelected; [self.titleBtn setTitle:model.name forState:(UIControlStateNormal)]; self.selectedBgBtn.selected = model.isSelected; self.selectedSoundBtn.selected = model.isSelected; self.iconImageView.image = [UIImage imageNamed:model.icon]; } -(void)initSubviews{ self.selectedBgBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)]; [self.selectedBgBtn setBackgroundImage:[UIImage imageNamed:@"room_sound_sel"] forState:(UIControlStateSelected)]; [self.selectedBgBtn setBackgroundImage:[UIImage imageWithColor:RGB16A(0xE9E9E9, 0.2)] forState:(UIControlStateNormal)]; [self.selectedBgBtn addRoundedCornersWithRadius:12]; self.selectedBgBtn.userInteractionEnabled = NO; [self.contentView addSubview:self.selectedBgBtn]; self.selectedSoundBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width-14-5, 5, 14, 14)]; [self.selectedSoundBtn setBackgroundImage:[UIImage imageNamed:@"room_sound_icon_sel"] forState:(UIControlStateSelected)]; [self.selectedSoundBtn setBackgroundImage:[UIImage imageNamed:@"room_sound_icon_nor"] forState:(UIControlStateNormal)]; self.selectedSoundBtn.userInteractionEnabled = NO; [self.contentView addSubview:self.selectedSoundBtn]; self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width-ScaleWidth(48))/2, 15, ScaleWidth(48), ScaleWidth(48))]; [self.contentView addSubview:self.iconImageView]; self.titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.iconImageView.bottom, self.width, 21)]; [self.titleBtn setTitleColor:QXConfig.textColor forState:(UIControlStateSelected)]; [self.titleBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)]; self.titleBtn.titleLabel.font = [UIFont systemFontOfSize:14]; self.titleBtn.userInteractionEnabled = NO; [self.contentView addSubview:self.titleBtn]; } @end @implementation QXSoundListModel @end