iOS学习笔记----NSTimer(基本使用,DefaultRunLoopMode,NSRunLoopCommonModes,准确性)
2014-08-23 18:24
483 查看
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>
上面两种方式实质上是一样的,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)不好意思还没学到,学到在更新……
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)不好意思还没学到,学到在更新……
相关文章推荐
- NSRunLoopCommonModes和NSDefaultRunLoopMode区别时发现
- 在寻找NSRunLoopCommonModes和NSDefaultRunLoopMode区别时发现
- [iOS学习笔记]runloop runMode方法调研
- IOS学习笔记(七)之UISegmentedControl分段控件的基本概念和使用方法
- iOS学习笔记-137.RunLoop05——Runloop相关类3_CFRunLoopTimerRef(NSTimer)为何定时有时会失败
- iOS学习笔记-125.SDWebImage1——基本使用
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法
- IOS学习笔记 CALayer & 核心动画 CALayer基本属性的使用(1)
- iOS学习笔记-039.UISearBar——基本使用
- IOS学习笔记(八)之UIActivityIndicatorView(活动指示器视图)的基本概念和使用方法
- iOS学习笔记-023.UIDatePicker的基本使用
- IOS学习笔记(十)之UIImageView图片视图的基本概念和使用方法
- IOS下的 NSTimer与Run loop Modes的使用和区别
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法
- IOS学习笔记(七)之UISegmentedControl分段控件的基本概念和使用方法
- 第三章、一些基本数据结构使用和解析(iOS学习笔记,从零开始。)
- IOS学习笔记(八)之UIActivityIndicatorView(活动指示器视图)的基本概念和使用方法
- IOS开发基础教程学习笔记3 使用Tab Bar切换视图
- IOS学习笔记33—XML解析之KissXML的使用