NSTimer知識點和倒計時的實現


 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,即使timerfiring 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]);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM