YOUPAILZMusicLyrieView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // YOUPAILZMusicLyrieView.m
  3. // YOUQU
  4. //
  5. // Created by CY on 2021/12/11.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAILZMusicLyrieView.h"
  9. #import "YOUPAILZMusicLyricCell.h"
  10. #import "YOUPAILZMusicModel.h"
  11. @interface YOUPAILZMusicLyrieView () <UITableViewDataSource,UITableViewDelegate>
  12. @property (nonatomic, weak) UIImageView *youpaipcoverImgV;
  13. @property (nonatomic, weak) UITableView *youpaiptableView;
  14. @property (nonatomic, strong) YOUPAILZMusicModel *youpaipmusicModel;
  15. @property (nonatomic, assign) NSInteger youpaipcurrentRow;
  16. @property (nonatomic, strong) NSTimer *youpaiptimer;
  17. @end
  18. @implementation YOUPAILZMusicLyrieView
  19. - (instancetype)initWithFrame:(CGRect)frame{
  20. if (self = [super initWithFrame:frame]) {
  21. [self youpaifinitUI];
  22. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicStateChangeNotification) name:@"VQUMusicStateChangeNotification" object:nil];
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(youpaifmusicInfoChangeNotification:) name:@"VQUMusicInfoChangeNotification" object:nil];
  24. }
  25. return self;
  26. }
  27. /// 播放状态发生改变
  28. - (void)youpaifmusicStateChangeNotification{
  29. if ([YOUPAILZMusicManager shareManager].youpaipisPlay) {
  30. [self youpaifresumeAnimation];
  31. }else{
  32. [self youpaifpauseAnimation];
  33. }
  34. self.youpaiptableView.hidden = ![YOUPAILZMusicManager shareManager].youpaipisShowLyrie;
  35. if (!self.youpaiptableView.hidden) {
  36. self.mj_x = KScreenWidth - (KScreenWidth * 0.5f + 8.0f);
  37. self.mj_w = KScreenWidth * 0.5f;
  38. }else{
  39. self.mj_x = KScreenWidth - (68.0f + 8.0f);
  40. self.mj_w = 68.0f;
  41. }
  42. }
  43. /// 音乐信息发生改变
  44. - (void)youpaifmusicInfoChangeNotification:(NSNotification *)noti{
  45. [self youpaifreloadMusicModel:noti.object];
  46. }
  47. - (void)youpaifinitUI{
  48. UIImageView *youpaipcoverImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"vqu_image_music_record"]];
  49. youpaipcoverImgV.layer.cornerRadius = 30.0f;
  50. youpaipcoverImgV.contentMode = UIViewContentModeScaleAspectFill;
  51. youpaipcoverImgV.clipsToBounds = YES;
  52. [self addSubview:youpaipcoverImgV];
  53. self.youpaipcoverImgV = youpaipcoverImgV;
  54. [youpaipcoverImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  55. make.right.offset(0.0f);
  56. make.centerY.equalTo(self);
  57. make.size.mas_offset(CGSizeMake(60.0f, 60.0f));
  58. }];
  59. UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  60. tableView.delegate = self;
  61. tableView.dataSource = self;
  62. tableView.userInteractionEnabled = NO;
  63. tableView.rowHeight = 34.0f;
  64. tableView.estimatedRowHeight = 34.0f;
  65. tableView.estimatedSectionHeaderHeight = 0.0f;
  66. tableView.estimatedSectionFooterHeight = 0.0f;
  67. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  68. tableView.backgroundColor = [UIColor clearColor];
  69. tableView.showsVerticalScrollIndicator = NO;
  70. tableView.clipsToBounds = YES;
  71. tableView.hidden = YES;
  72. [self addSubview:tableView];
  73. self.youpaiptableView = tableView;
  74. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.left.bottom.offset(0.0f);
  76. make.top.offset(0.0f);
  77. make.right.equalTo(youpaipcoverImgV.mas_left).offset(-8.0f);
  78. }];
  79. }
  80. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  81. if (self.youpaipmusicModel.youpaipwordArray == nil) {
  82. return 0;
  83. }
  84. return self.youpaipmusicModel.youpaipwordArray.count;
  85. }
  86. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  87. NSString *cellID = @"YOUPAILZMusicLyricCell";
  88. YOUPAILZMusicLyricCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  89. if (cell == nil) {
  90. cell = [[YOUPAILZMusicLyricCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
  91. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  92. }
  93. NSString *text = self.youpaipmusicModel.youpaipwordArray[indexPath.row];
  94. text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
  95. text = [text stringByReplacingOccurrencesOfString:@"\r" withString:@""];
  96. text = [text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  97. [self youpaifsetupShadowWithText:text view:cell.youpaiptextL];
  98. if(indexPath.row == self.youpaipcurrentRow){
  99. cell.youpaiptextL.textColor = [UIColor whiteColor];
  100. cell.youpaiptextL.font = LCBoldFont(14.0f);
  101. }else{
  102. cell.youpaiptextL.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8f];
  103. cell.youpaiptextL.font = LCFont14;
  104. }
  105. cell.youpaiptextL.textAlignment = NSTextAlignmentRight;
  106. cell.youpaiptextL.numberOfLines = 2;
  107. return cell;
  108. }
  109. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  110. return CGFLOAT_MIN;
  111. }
  112. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  113. return CGFLOAT_MIN;
  114. }
  115. - (void)youpaifupdateTime{
  116. CGFloat currentTime = currentTime = [[YOUPAILZChatRoomManager shareManager].agoraKit getAudioMixingCurrentPosition] * 0.001f;
  117. NSLog(@"%d:%d",(int)currentTime / 60, (int)currentTime % 60);
  118. for (int i = 0; i < self.youpaipmusicModel.youpaiptimerArray.count; i ++) {
  119. NSArray *timeArray=[self.youpaipmusicModel.youpaiptimerArray[i] componentsSeparatedByString:@":"];
  120. float youpaiplrcTime = [timeArray[0] intValue] * 60 + [timeArray[1] floatValue];
  121. if(currentTime>youpaiplrcTime){
  122. self.youpaipcurrentRow = i;
  123. }else
  124. break;
  125. }
  126. [self.youpaiptableView reloadData];
  127. if (self.youpaipmusicModel.youpaipwordArray.count != 0 && self.youpaipcurrentRow < self.youpaipmusicModel.youpaipwordArray.count) {
  128. [self.youpaiptableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.youpaipcurrentRow inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  129. }
  130. }
  131. - (void)youpaifreloadMusicModel:(YOUPAILZMusicModel *)youpaipmusicModel{
  132. self.youpaipmusicModel = nil;
  133. self.youpaipcurrentRow = 0;
  134. self.youpaipcoverImgV.image = [UIImage imageNamed:@"vqu_image_music_record"];
  135. [self.youpaiptableView reloadData];
  136. if (self.youpaiptimer == nil) {
  137. [self youpaifstartTimer];
  138. }
  139. self.youpaipmusicModel = youpaipmusicModel;
  140. [self.youpaipcoverImgV sd_setImageWithURL:[NSURL URLWithString:youpaipmusicModel.youpaipposter]];
  141. [self.youpaiptableView reloadData];
  142. }
  143. - (void)youpaifstartTimer{
  144. self.youpaiptimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(youpaifupdateTime) userInfo:nil repeats:YES];
  145. [[NSRunLoop currentRunLoop] addTimer:self.youpaiptimer forMode:NSRunLoopCommonModes];
  146. }
  147. //暂停动画
  148. - (void)youpaifpauseAnimation {
  149. [self.youpaipcoverImgV.layer removeAnimationForKey:@"rotationAnimation"];
  150. }
  151. //恢复动画
  152. - (void)youpaifresumeAnimation {
  153. CABasicAnimation *rotationAnimation;
  154. rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  155. rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
  156. rotationAnimation.duration = 10.0f;
  157. rotationAnimation.cumulative = YES;
  158. rotationAnimation.repeatCount = CGFLOAT_MAX;
  159. rotationAnimation.removedOnCompletion = NO;
  160. [self.youpaipcoverImgV.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
  161. }
  162. - (void)youpaifsetupShadowWithText:(NSString *)text view:(UILabel *)view{
  163. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
  164. NSShadow *shadow = [[NSShadow alloc]init];
  165. shadow.shadowOffset = CGSizeMake(1, 1.5f);
  166. shadow.shadowColor = [HexColorFromRGB(0x000000) colorWithAlphaComponent:0.2f];
  167. [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
  168. view.attributedText = attributedString;
  169. }
  170. - (void)dealloc{
  171. [self youpaifstopTimer];
  172. [[NSNotificationCenter defaultCenter] removeObserver:self];
  173. }
  174. - (void)youpaifstopTimer{
  175. [self.youpaiptimer invalidate];
  176. self.youpaiptimer = nil;
  177. }
  178. - (void)youpaifclose{
  179. [self youpaifstopTimer];
  180. self.youpaipmusicModel = nil;
  181. [self.youpaiptableView reloadData];
  182. self.youpaipcoverImgV.image = [UIImage imageNamed:@"vqu_image_music_record"];
  183. }
  184. @end