一.歌词的展示 -- 首先歌词是在scrollView上,scrollView的大小是两个屏幕的宽度

  • scrollView滚动修改透明度的代码                                                            
  • 自定义展示歌词的view,继承自UIScrollView,向外界提供一个歌词文件名的属性

    /** 歌词文件的名字 */

    @property(nonatomic,copy) NSString *lrcFileName;

    重写setter,解析歌词,通过歌词的工具类获得模型集合

  • 歌词工具类的实现
     #import "ChaosLrcTool.h"
    #import "ChaosLrc.h" @implementation ChaosLrcTool
    + (NSArray *)lrcToolWithLrcName:(NSString *)lrcname
    {
    // 1.获取文件路径
    NSString *lrcPath = [[NSBundle mainBundle] pathForResource:lrcname ofType:nil];
    // 2.加载文件内容
    NSString *lrcString = [NSString stringWithContentsOfFile:lrcPath encoding:NSUTF8StringEncoding error:nil];
    // 3.切割字符串
    NSArray *lrcLines = [lrcString componentsSeparatedByString:@"\n"];
    // 4.遍历集合,转换成歌词模型
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *lrcLineString in lrcLines) {
    /*
    [ti:简单爱]
    [ar:周杰伦]
    [al:范特西]
    */
    // 5.跳过指定行
    if ([lrcLineString hasPrefix:@"[ti"] || [lrcLineString hasPrefix:@"[ar"] || [lrcLineString hasPrefix:@"[al"] || ![lrcLineString hasPrefix:@"["]) {
    continue;
    }
    ChaosLrc *lrc = [ChaosLrc lrcWithLrcLine:lrcLineString];
    [arr addObject:lrc];
    }
    return arr;
    }
    @end
  • 歌词解析完毕后,根据歌词模型集合来实现tableView(歌词用tableView来显示)的数据源方法

二.歌词的滚动

  • 歌词的滚动由每一句的时间来决定,自定义的歌词的view需要外界不停的提供歌曲播放的时间,自己来判断并滚动显示对应的歌词.所以自定义的歌词View需要向外界提供一个时间属性,重写时间属性来实现歌词滚动

     #pragma mark - 歌词滚动
    // 重写time的setter
    - (void)setCurrentTime:(NSTimeInterval)currentTime
    {
    _currentTime = currentTime;
    // 遍历歌词,找到对应时间应该显示的歌词模型
    for (int i = ; i < self.lrcList.count; i++) {
    // 当前的歌词
    ChaosLrc *lrc = self.lrcList[i];
    NSInteger next = i + ;
    // 下一句歌词
    ChaosLrc *nextLrc;
    if (next < self.lrcList.count) {
    nextLrc = self.lrcList[next];
    }
    if (self.currentLrcIndex != i && currentTime >= lrc.time && currentTime < nextLrc.time) { NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:];
    NSIndexPath *previousIndexPath = [NSIndexPath indexPathForRow:self.currentLrcIndex inSection:];
    // 记录当前行
    self.currentLrcIndex = i;
    // 满足条件,tableview滚动
    [self.lrcView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    // 刷新上一行,字体还原
    [self.lrcView reloadRowsAtIndexPaths:@[previousIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    // 刷新当前行,字体变大
    [self.lrcView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
    // 当前歌词的label的进度
    if (self.currentLrcIndex == i) { // 获取当前的cell
    NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:i inSection:];
    ChaosLrcCell *currentCell = [self.lrcView cellForRowAtIndexPath:currentIndexPath];
    CGFloat totalTime = nextLrc.time - lrc.time; // 当前歌词总时间
    CGFloat progressTime = currentTime - lrc.time; // 当前歌词已经走过了多长时间
    currentCell.lrcLabel.progress = progressTime / totalTime; // 主页的label
    self.mainLabel.text = lrc.text;
    self.mainLabel.progress = currentCell.lrcLabel.progress;
    }
    }
    }
  • 外界给歌词的View提供时间就不是每一秒提供一次那么简单了,外界需要更牛逼的定时器                                     
    • 播放的时候添加定时器                              
    • 定时器的方法中实现给歌词的view提供的时间属性赋值

三.歌词的颜色变化 -- 画上去的(自定义cell中的显示歌词的label,label需要外界提供一个进度值,自己内部根据进度值来画)

  • 自定义label的实现

     #import "ChaosLabel.h"
    
     @implementation ChaosLabel
    
     - (void)setProgress:(CGFloat)progress
    {
    _progress = progress;
    // 重绘
    [self setNeedsDisplay];
    } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; // 1.获取需要画的区域
    CGRect fillRect = CGRectMake(, , self.bounds.size.width * self.progress, self.bounds.size.height);
    // 2.选择颜色
    [[UIColor yellowColor] set];
    // 3.添加区域开始画图
    // UIRectFill(fillRect);
    UIRectFillUsingBlendMode(fillRect, kCGBlendModeSourceIn);
    } @end
  • 外部进度值的计算

iOS开发--QQ音乐练习,歌词的展示,歌词的滚动,歌词的颜色变化的更多相关文章

  1. iOS开发--QQ音乐练习,后台播放和锁屏界面

    一.设置后台播放 首先允许程序后台播放 代码实现 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti ...

  2. iOS开发--QQ音乐练习,旋转动画的实现,音乐工具类的封装,定时器的使用技巧,SliderBar的事件处理

    一.旋转动画的实现 二.音乐工具类的封装 -- 返回所有歌曲,返回当前播放歌曲,设置当前播放歌曲,返回下一首歌曲,返回上一首歌曲方法的实现 头文件 .m文件 #import "ChaosMu ...

  3. 10、 在QQ音乐中爬取某首歌曲的歌词

        需求就是把关卡内的代码稍作修改,将周杰伦前五页歌曲的歌词都爬取下来,结果就是全部展示打印出来.       URL  https://y.qq.com/portal/search.html#p ...

  4. iOS开发 QQ粘性动画效果

    QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00  博客园精华区 原文  http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...

  5. IOS 开发qq登陆界面

    // //  ViewController.m //  QQUI_bydfg // //  Created by Kevin_dfg on 16/4/15. //  Copyright © 2016年 ...

  6. iOS开发QQ空间半透明效果的实现

    //1.首先我们可以确定的是cell的半透明, /* white The grayscale value of the color object, specified as a value from ...

  7. iOS开发所有KeyboardType与图片对应展示

    1.UIKeyboardTypeAlphabet 2.UIKeyboardTypeASCIICapable 3.UIKeyboardTypeDecimalPad  4.UIKeyboardTypeDe ...

  8. iOS开发-- 如何让 UITableView 的 headerView跟随 cell一起滚动

    在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特. 1. 所以我就把它 作为一个section的 headerView. 也就 ...

  9. iOS开发技巧(系列十八:扩展UIColor,支持十六进制颜色设置)

    新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置. UIColor+Hex.h文件, #import <UIKit/UIKit.h> # ...

随机推荐

  1. dedecms调用标签总结(一)

    dedecms 基本包含了一个常规网站需要的一切功能,拥有完善的中文学习资料,很容易上手,学习成本较低.学会dedecms 的模板修改.栏目新增.内容模型新增和常用的标签调用方法后,即便我们不懂 ph ...

  2. AngularJS 控制器

    AngularJS 控制器 控制 AngularJS 应用程序的数据. AngularJS 控制器是常规的 JavaScript 对象. AngularJS 控制器 AngularJS 应用程序被控制 ...

  3. 分页查询的两种方法(双top 双order 和 row_number() over ())

    --跳过10条取2条 也叫分页select top 2 * from studentwhere studentno not in (select top 2 studentno from studen ...

  4. 终于可以在centos下使用QQ啦!

    电脑装了centos 6.4操作系统,一直无法使用QQ,在centos中文论坛看到一篇介绍安装qq的文章,依样画葫芦,终于成功了1.下载QQ2012软件安装包,我给大家准备好了下载地址 [root@b ...

  5. [No000066]python各种类型转换-int,str,char,float,ord,hex,oct等

    int(x [,base ]) #将x转换为一个整数 long(x [,base ]) #将x转换为一个长整数 float(x ) #将x转换到一个浮点数 complex(real [,imag ]) ...

  6. preg_match()漏洞

    今天大哥丢了一道题过来. <?php $str = intval($_GET['id']); $reg = preg_match('/\d/is', $_GET['id']); //有0-9的数 ...

  7. BZOJ 2724: [Violet 6]蒲公英

    2724: [Violet 6]蒲公英 Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 1633  Solved: 563[Submit][Status ...

  8. django复习笔记3:实战

    1.初始化 2.配置后台,增加测试数据 3.测试urls/views/templates 4.增加静态资源 5.修改样式 6.模版继承 7.增加博文主页 8.增加表单 9.完善新增页面和编辑页面的表单 ...

  9. php 正则匹配中文(转)

    我使用正则表达式来匹配中问的时候,出现了无法匹配的问题,问题如下 PCRE does not support \L, \l, \N{name}, \U, or \u at offset 2 我原来的匹 ...

  10. poj3270

    Cow Sorting Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6750   Accepted: 2633 Descr ...