// // YOUPAILZMusicLyrieView.m // YOUQU // // Created by CY on 2021/12/11. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAILZMusicLyrieView.h" #import "YOUPAILZMusicLyricCell.h" #import "YOUPAILZMusicModel.h" @interface YOUPAILZMusicLyrieView () @property (nonatomic, weak) UIImageView *youpaipcoverImgV; @property (nonatomic, weak) UITableView *youpaiptableView; @property (nonatomic, strong) YOUPAILZMusicModel *youpaipmusicModel; @property (nonatomic, assign) NSInteger youpaipcurrentRow; @property (nonatomic, strong) NSTimer *youpaiptimer; @end @implementation YOUPAILZMusicLyrieView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self youpaifinitUI]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicStateChangeNotification) name:@"VQUMusicStateChangeNotification" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicInfoChangeNotification:) name:@"VQUMusicInfoChangeNotification" object:nil]; } return self; } /// 播放状态发生改变 - (void)youpaifmusicStateChangeNotification{ if ([YOUPAILZMusicManager shareManager].youpaipisPlay) { [self youpaifresumeAnimation]; }else{ [self youpaifpauseAnimation]; } self.youpaiptableView.hidden = ![YOUPAILZMusicManager shareManager].youpaipisShowLyrie; if (!self.youpaiptableView.hidden) { self.mj_x = KScreenWidth - (KScreenWidth * 0.5f + 8.0f); self.mj_w = KScreenWidth * 0.5f; }else{ self.mj_x = KScreenWidth - (68.0f + 8.0f); self.mj_w = 68.0f; } } /// 音乐信息发生改变 - (void)youpaifmusicInfoChangeNotification:(NSNotification *)noti{ [self youpaifreloadMusicModel:noti.object]; } - (void)youpaifinitUI{ UIImageView *youpaipcoverImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vqu_image_music_record"]]; youpaipcoverImgV.layer.cornerRadius = 30.0f; youpaipcoverImgV.contentMode = UIViewContentModeScaleAspectFill; youpaipcoverImgV.clipsToBounds = YES; [self addSubview:youpaipcoverImgV]; self.youpaipcoverImgV = youpaipcoverImgV; [youpaipcoverImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.right.offset(0.0f); make.centerY.equalTo(self); make.size.mas_offset(CGSizeMake(60.0f, 60.0f)); }]; UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.userInteractionEnabled = NO; tableView.rowHeight = 34.0f; tableView.estimatedRowHeight = 34.0f; tableView.estimatedSectionHeaderHeight = 0.0f; tableView.estimatedSectionFooterHeight = 0.0f; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.backgroundColor = [UIColor clearColor]; tableView.showsVerticalScrollIndicator = NO; tableView.clipsToBounds = YES; tableView.hidden = YES; [self addSubview:tableView]; self.youpaiptableView = tableView; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.offset(0.0f); make.top.offset(0.0f); make.right.equalTo(youpaipcoverImgV.mas_left).offset(-8.0f); }]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (self.youpaipmusicModel.youpaipwordArray == nil) { return 0; } return self.youpaipmusicModel.youpaipwordArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString *cellID = @"YOUPAILZMusicLyricCell"; YOUPAILZMusicLyricCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (cell == nil) { cell = [[YOUPAILZMusicLyricCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } NSString *text = self.youpaipmusicModel.youpaipwordArray[indexPath.row]; text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符 text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@""]; text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""]; [self youpaifsetupShadowWithText:text view:cell.youpaiptextL]; if(indexPath.row == self.youpaipcurrentRow){ cell.youpaiptextL.textColor = [UIColor whiteColor]; cell.youpaiptextL.font = LCBoldFont(14.0f); }else{ cell.youpaiptextL.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8f]; cell.youpaiptextL.font = LCFont14; } cell.youpaiptextL.textAlignment = NSTextAlignmentRight; cell.youpaiptextL.numberOfLines = 2; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return CGFLOAT_MIN; } - (void)youpaifupdateTime{ CGFloat currentTime = currentTime = [[YOUPAILZChatRoomManager shareManager].agoraKit getAudioMixingCurrentPosition] * 0.001f; NSLog(@"%d:%d",(int)currentTime / 60, (int)currentTime % 60); for (int i = 0; i < self.youpaipmusicModel.youpaiptimerArray.count; i ++) { NSArray *timeArray=[self.youpaipmusicModel.youpaiptimerArray[i] componentsSeparatedByString:@":"]; float youpaiplrcTime = [timeArray[0] intValue] * 60 + [timeArray[1] floatValue]; if(currentTime>youpaiplrcTime){ self.youpaipcurrentRow = i; }else break; } [self.youpaiptableView reloadData]; if (self.youpaipmusicModel.youpaipwordArray.count != 0 && self.youpaipcurrentRow < self.youpaipmusicModel.youpaipwordArray.count) { [self.youpaiptableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.youpaipcurrentRow inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } } - (void)youpaifreloadMusicModel:(YOUPAILZMusicModel *)youpaipmusicModel{ self.youpaipmusicModel = nil; self.youpaipcurrentRow = 0; self.youpaipcoverImgV.image = [UIImage imageNamed:@"vqu_image_music_record"]; [self.youpaiptableView reloadData]; if (self.youpaiptimer == nil) { [self youpaifstartTimer]; } self.youpaipmusicModel = youpaipmusicModel; [self.youpaipcoverImgV sd_setImageWithURL:[NSURL URLWithString:youpaipmusicModel.youpaipposter]]; [self.youpaiptableView reloadData]; } - (void)youpaifstartTimer{ self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(youpaifupdateTime) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes]; } //暂停动画 - (void)youpaifpauseAnimation { [self.youpaipcoverImgV.layer removeAnimationForKey:@"rotationAnimation"]; } //恢复动画 - (void)youpaifresumeAnimation { CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ]; rotationAnimation.duration = 10.0f; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = CGFLOAT_MAX; rotationAnimation.removedOnCompletion = NO; [self.youpaipcoverImgV.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } - (void)youpaifsetupShadowWithText:(NSString *)text view:(UILabel *)view{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text]; NSShadow *shadow = [[NSShadow alloc]init]; shadow.shadowOffset = CGSizeMake(1, 1.5f); shadow.shadowColor = [HexColorFromRGB(0x000000) colorWithAlphaComponent:0.2f]; [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)]; view.attributedText = attributedString; } - (void)dealloc{ [self youpaifstopTimer]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)youpaifstopTimer{ [self.youpaiptimer invalidate]; self.youpaiptimer = nil; } - (void)youpaifclose{ [self youpaifstopTimer]; self.youpaipmusicModel = nil; [self.youpaiptableView reloadData]; self.youpaipcoverImgV.image = [UIImage imageNamed:@"vqu_image_music_record"]; } @end