NSTimer学习笔记
2015-07-28 11:19
477 查看
NSTimer其实是将一个监听加入的系统的RunLoop中去,当系统runloop到如何timer条件的循环时,会调用timer一次,当timer执行完,也就是回调函数执行之后,timer会再一次的将自己加入到runloop中去继续监听。
CFRunLoopTimerRef 和 NSTimer这两个类型是可以互换的, 当我们在传参数的时候,看到CFRunLoopTimerRef可以传NSTimer的参数,增加强制转化来避免编译器的警告信息
指定(注册)一个timer到 RunLoops中
一个timer对象只能够被注册到一个runloop中在同一时间,尽管在这个runloop中它能够被添加到多个runloop中模式中去。
有以下三种方法:
使用 scheduledTimerWithTimeInterval:invocation:repeats: 或者scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 这两个类方法创建一个timer并把它指定到一个默认的runloop模式中
使用 timerWithTimeInterval:invocation:repeats: 或者 timerWithTimeInterval:target:selector:userInfo:repeats: 这两个类方法创建一个timer的对象,不把它知道那个到run
loop. (当创建之后,你必须手动的调用NSRunLoop下对应的方法 addTimer:forMode: 去将它制定到一个runloop模式中.)
使用 initWithFireDate:interval:target:selector:userInfo:repeats: 方法分配并创建一个NSTimer的实例 (当创建之后,你必须手动的调用NSRunLoop下对应的方法 addTimer:forMode: 去将它制定到一个runloop模式中.)
[cpp] view
plaincopy
// 安装timer(注册timer)
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 5// 当函数正在调用时,及时间隔时间到了 也会忽略此次调用
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES]; // 如果是NO 不重复,则timer在触发了回调函数调用完成之后 会自动释放这个timer,以免timer被再一次的调用,如果是YES,则会重复调用函数,调用完函数之后,会将这个timer加到RunLoop中去,等待下一次的调用,知道用户手动释放timer( [timer invalidate];)。
[cpp] view
plaincopy
- (void) handleTimer: (NSTimer *) timer // timer的回调函数
{
//在这里进行处理
NSLog(@"1");
// for (int i = 0; i <= 1000000000; )
// {
// i++;
// }
}
[cpp] view
plaincopy
// [timer invalidate]; // 这个函数将timer从当前的RunLoop中remove掉,必须在timer安装的线程中调用这个函数。
[cpp] view
plaincopy
[timer fire];// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达
//timer 扫描本地设备
self.ctrlTimer = [NSTimer
timerWithTimeInterval:2
target:self
selector:@selector(scheduledForLocalDevices:)
userInfo:nil
repeats:YES];
[[NSRunLoop
currentRunLoop] addTimer:self.ctrlTimer
forMode:NSDefaultRunLoopMode];
[self.ctrlTimer
fire];
CFRunLoopTimerRef 和 NSTimer这两个类型是可以互换的, 当我们在传参数的时候,看到CFRunLoopTimerRef可以传NSTimer的参数,增加强制转化来避免编译器的警告信息
指定(注册)一个timer到 RunLoops中
一个timer对象只能够被注册到一个runloop中在同一时间,尽管在这个runloop中它能够被添加到多个runloop中模式中去。
有以下三种方法:
使用 scheduledTimerWithTimeInterval:invocation:repeats: 或者scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 这两个类方法创建一个timer并把它指定到一个默认的runloop模式中
使用 timerWithTimeInterval:invocation:repeats: 或者 timerWithTimeInterval:target:selector:userInfo:repeats: 这两个类方法创建一个timer的对象,不把它知道那个到run
loop. (当创建之后,你必须手动的调用NSRunLoop下对应的方法 addTimer:forMode: 去将它制定到一个runloop模式中.)
使用 initWithFireDate:interval:target:selector:userInfo:repeats: 方法分配并创建一个NSTimer的实例 (当创建之后,你必须手动的调用NSRunLoop下对应的方法 addTimer:forMode: 去将它制定到一个runloop模式中.)
[cpp] view
plaincopy
// 安装timer(注册timer)
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 5// 当函数正在调用时,及时间隔时间到了 也会忽略此次调用
target: self
selector: @selector(handleTimer:)
userInfo: nil
repeats: YES]; // 如果是NO 不重复,则timer在触发了回调函数调用完成之后 会自动释放这个timer,以免timer被再一次的调用,如果是YES,则会重复调用函数,调用完函数之后,会将这个timer加到RunLoop中去,等待下一次的调用,知道用户手动释放timer( [timer invalidate];)。
[cpp] view
plaincopy
- (void) handleTimer: (NSTimer *) timer // timer的回调函数
{
//在这里进行处理
NSLog(@"1");
// for (int i = 0; i <= 1000000000; )
// {
// i++;
// }
}
[cpp] view
plaincopy
// [timer invalidate]; // 这个函数将timer从当前的RunLoop中remove掉,必须在timer安装的线程中调用这个函数。
[cpp] view
plaincopy
[timer fire];// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达
//timer 扫描本地设备
self.ctrlTimer = [NSTimer
timerWithTimeInterval:2
target:self
selector:@selector(scheduledForLocalDevices:)
userInfo:nil
repeats:YES];
[[NSRunLoop
currentRunLoop] addTimer:self.ctrlTimer
forMode:NSDefaultRunLoopMode];
[self.ctrlTimer
fire];
相关文章推荐
- 思考的工作方式——计划经济or市场经济
- 解决vs2013不能添加控制器的步骤
- css display 总结
- Linux上jdk的安装(CentOS6.5)
- Netty入门教程
- HDU 2014青年歌手大奖赛_评委会打
- java使用telnet连接远程计算机执行命令
- 在iOS8系统下,UITabbar上的item自定义背景色和图片问题
- 创业有风险,招聘需谨慎
- centos6.4 安装 svn
- 两种oracle创建字段自增长的实现方式
- 认识Notification通知栏通知
- Python3访问并下载网页内容的方法
- 实现oracle数据库字段自增长(两种方式)
- StartActivityforResult 的讲解转载的
- NSSortDescriptor排序(刚刚)
- 深入阅读java api之URL
- 读Spring的源代码四:获取bean实例的过程
- 24点
- tableView下面多余显示的Cell去除方法