iOS学习笔记----NSTimer(基本使用,DefaultRunLoopMode,NSRunLoopCommonModes,准确性)

来源:互联网 发布:数据分析看什么书 编辑:程序博客网 时间:2024/06/03 05:51

NSTimer:

 

TimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;


     1. 时间间隔,double

     2. 监听时钟触发的对象

     3. 调用方法

     4. userInfo,可以是任意对象,通常传递nil

     5. repeats:是否重复




1.

<span style="font-size:18px;">[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:@"hello timer" repeats:YES];</span>

<span style="font-size:18px;">[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];</span>

上面两种方式实质上是一样的,
scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟, 添加到运行循环的模式是DefaultRunLoopMode.


2.

self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

timer添加到运行循环

模式:NSRunLoopCommonModes的运行循环模式(监听滚动模式)


3.DefaultRunLoopMode:当我们执行其他滚动事件时,timer会默认暂时不监听,当滚动结束的时候会,继续监听。

NSRunLoopCommonModes:始终监听滚动事件


4.NSTimer准确性:

当程序执行的时候,遇到cpu忙碌的时候,NSTimer会被放到一边不执行,就会造成该执行的事件不执行,会造成事件的叠加。

所以说NSTimer通常会用来做一些有一定时间跨度的时间处理。

解决办法:(1.多线程,2.CADdisplay link)不好意思还没学到,学到在更新……

0 0
原创粉丝点击