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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值