NSTimer知识点和倒计时的实现
2013-12-20 10:23
375 查看
NSTimer其实是将一个监听加入到系统的RunLoop中去,当系统runloop到如何timer条件的循环时,会调用timer一次,当timer执行完,也就是回调函数执行之后,timer会再一次的将自己加入到runloop中去继续监听。
CFRunLoopTimerRef 和 NSTimer这两个类型是可以互换的,
当我们在传参数的时候,看到CFRunLoopTimerRef可以传NSTimer的参数,增加强制转化来避免编译器的警告信息
一个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模式中.)
[timer fire];// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达
NSTimer 关闭 [time setFireDate:[NSDate distantFunture]]
开启 [time setFireDate:[NSDate distanPast]]
NSTimer官方类方法不多,就下面这么一点东西,就都搬上来了:
@interface NSTimer : NSObject
//初始化,最好用scheduled方式初始化,不然需要手动addTimer:forMode: 将timer添加到一个runloop中。
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;
- (void)fire; //立即触发定时器
- (NSDate *)fireDate;//开始时间
- (void)setFireDate:(NSDate *)date;//设置fireData,其实暂停、开始会用到
- (NSTimeInterval)timeInterval;//延迟时间
- (void)invalidate;//停止并删除
- (BOOL)isValid;//判断是否valid
- (id)userInfo;//通常用nil
@end
在invalidate之前最好先用isValid先判断是否还在线程中:
定时器暂停与继续的简要方法:
使用NSTimer实现倒计时的一个例子:
-(void)viewDidLoad中添加如下代码,每秒出发一次
CFRunLoopTimerRef 和 NSTimer这两个类型是可以互换的,
当我们在传参数的时候,看到CFRunLoopTimerRef可以传NSTimer的参数,增加强制转化来避免编译器的警告信息
一个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模式中.)
[timer fire];// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达
NSTimer 关闭 [time setFireDate:[NSDate distantFunture]]
开启 [time setFireDate:[NSDate distanPast]]
NSTimer官方类方法不多,就下面这么一点东西,就都搬上来了:
@interface NSTimer : NSObject
//初始化,最好用scheduled方式初始化,不然需要手动addTimer:forMode: 将timer添加到一个runloop中。
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;
- (void)fire; //立即触发定时器
- (NSDate *)fireDate;//开始时间
- (void)setFireDate:(NSDate *)date;//设置fireData,其实暂停、开始会用到
- (NSTimeInterval)timeInterval;//延迟时间
- (void)invalidate;//停止并删除
- (BOOL)isValid;//判断是否valid
- (id)userInfo;//通常用nil
@end
在invalidate之前最好先用isValid先判断是否还在线程中:
if ([scrollTimer isValid] == YES) { [scrollTimer invalidate]; scrollTimer = nil; }
定时器暂停与继续的简要方法:
[timer setFireDate:[NSDate date]]; //继续。 [timer setFireDate:[NSDate distantPast]];//开启 [timer setFireDate:[NSDate distantFuture]];//暂停
使用NSTimer实现倒计时的一个例子:
-(void)viewDidLoad中添加如下代码,每秒出发一次
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES]; //使用timer定时,每秒触发一次,然后就是写selector了。
- (void)timerFireMethod:(NSTimer*)theTimer { NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象 NSDateComponents *endTime = [[NSDateComponents alloc] init]; //初始化目标时间...奥运时间好了 [endTime setYear:2008]; [endTime setMonth:8]; [endTime setDay:8]; [endTime setHour:8]; [endTime setMinute:8]; [endTime setSecond:8]; NSDate *todate = [cal dateFromComponents:endTime]; //把目标时间装载入date [endTime release]; NSDate *today = [NSDate date]; //得到当前时间 //用来得到具体的时差 unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0]; NSLog(@"%d年%d月%d日%d时%d分%d秒",[d year],[d month], [d day], [d hour], [d minute], [d second]); }
相关文章推荐
- Twitter Storm的一些关键概念
- 分享知乎上一些有意思的帖子
- camera驱动 v4l2/fimc
- Objective--C语言预处理命令之条件编译(#ifdef,#else,#endif,#if等)
- Java EE应用分层
- Swoole
- Twitter Storm如何保证消息不丢失
- 这一年的总结——2013
- asp.net 对word的操作(读取,txt追加,替换)
- linux内核TCP相关参数解释
- Twitter Storm入门
- 窗口消息的传递
- Unity3D基础学习 NGUI自带Tooltip制作提示文字
- VS 2010调试程序,怎么查看其中变量的值
- 初学者如何在 Linux (Ubuntu) 下搭建C/C++ 开发环境
- 我的IT生活-量子
- 在Java中使用SQLite的教程(转)
- Understanding a Kernel Oops!
- 同事写的图片压缩方法
- 3D格式简介