• iOS开发--计时器-NSTimer与CADisplayLink


    如果程序要让某个方法重复执行,可以借助定时器来完成。CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器,NSTimer的精确度低了点,比如NSTimer的触发时间到的时候,runloop如果在阻塞状态,触发时间就会推迟到下一个runloop周期。本章节我们来看一下这两种类的基本用法。

    一、NSTimer属性方法介绍

    • TimeInterval:指定每隔多少秒执行一次任务。
    • invalidate:终止计时器
    1. + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;

    调用上面类方法可以创建NSTimer对象,它总共包含了5个参数:

    • scheduledTimerWithTimeInterval:指定每隔多长时间执行一次任务。
    • target与selector:指定重复执行的任务。
    • userInfo:该参数用于传入额外的附加信息,可以设置为nil。
    • userInfo repeats:指定一个BOOL值,控制是否重复执行任务。

    二、CADisplayLink属性方法介绍

    CADisplayLink使用场合相对专一,适合做UI的不停重绘,比如自定义动画引擎或者视频播放的渲染。NSTimer的使用范围要广泛的多,各种需要单次或者循环定时处理的任务都可以使用。在UI相关的动画或者显示内容使用CADisplayLink比起用NSTimer的好处就是我们不需要在格外关心屏幕的刷新频率了,因为它本身就是跟屏幕刷新同步的。

    • invalidate:终止计时器。
    • duration:提供了每帧之间的时间,也就是每次任务刷新之间的的时间。
    1. - (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode;//用来将CADisplayLink对象加入到runloop中。
    1. + (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)see;//指定重复执行的任务

    三、示例代码

    • 在故事板中添加两个按钮,用来指定要开始的计时器类型,并与它所属控制器建立关联

    QQ20160408-0

    • 懒加载方式初始化用到的对象
    1. @property (nonatomic, assign) NSInteger count;//记录任务执行的次数
    2. @property (nonatomic, strong) NSTimer *myTimer;
    3. @property (nonatomic, strong) CADisplayLink *myDisplayLink;
    1. - (CADisplayLink *)myDisplayLink{
    2. if (!_myDisplayLink) {
    3. _myDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLink)];
    4. [self.myDisplayLink addToRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
    5. }
    6. return _myDisplayLink;
    7. }
    8. - (void)displayLink{
    9. NSLog(@"CAD正在执行第%ld此任务",self.count++);
    10. //如果count值大于10就取消计时器
    11. if (self.count > 10) {
    12. NSLog(@"取消定时器");
    13. [self.myDisplayLink invalidate];
    14. self.count = 0;//将计数置为0,确保下一个计时器从0开始计数
    15. }
    16. }
    17. - (NSTimer *)myTimer{
    18. if (!_myTimer) {
    19. _myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(time) userInfo:nil repeats:YES];
    20. }
    21. return _myTimer;
    22. }
    23. - (void)time{
    24. NSLog(@"NSTimer正在执行第%ld此任务",self.count++);
    25. //如果count值大于10就取消计时器
    26. if (self.count > 10) {
    27. NSLog(@"取消定时器");
    28. [self.myTimer invalidate];
    29. self.count = 0;
    30. }
    31. }
    32. - (NSInteger) count{
    33. if (!_count) {
    34. _count = 1;
    35. }
    36. return _count;
    37. }
    38. - (void)viewDidLoad {
    39. [super viewDidLoad];
    40. [self count];
    41. }
    • 点击相应按钮方法,开始执行计时器
    1. - (IBAction)CADBtn:(id)sender {
    2. [self myDisplayLink];
    3. }
    4. - (IBAction)timerBtn:(id)sender {
    5. [self myTimer];
    6. }
  • 相关阅读:
    Error C1189: #error: Please use the /MD switch for _AFXDLL builds
    block,inline和inline-block概念和区别(转载)
    jQuery学习--Code Organization Concepts
    Kafka— —副本(均衡负载)
    Kafka实践1--Producer
    漫画HDFS工作原理(转)
    JavaScript学习笔记3
    搭建简单SBT工程实践
    Hive SQL测试
    SparkSql常用语句
  • 原文地址:https://www.cnblogs.com/wanghuaijun/p/5391328.html
Copyright © 2020-2023  润新知