您的位置:首页 > 其它

NSTimer知识点和倒计时的实现

2015-10-23 09:06 155 查看
原文链接:http://www.cnblogs.com/ios-wmm/archive/2012/08/24/2654779.html

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先判断是否还在线程中:

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]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: