iOS之三大定時器


iOS開發中定時器經常會用到,iOS中常用的定時器有三種,分別是NSTime,CADisplayLink和GCD。

一, NSTimer

方式1

    // 創建定時器
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES];
    // 停止定時器
    [timer invalidate];

方式2

    // 創建定時器
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(test) userInfo:nil repeats:YES];
    // 將定時器添加到runloop中,否則定時器不會啟動
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    // 停止定時器
    [timer invalidate];

方式1:會自動將創建的定時器以默認方式添加到當前線程runloop中,而無需手動添加。但是在此種模式下,當滾動屏幕時runloop會進入另外一種模式,定時器會暫停,為了解決這種問題,可以

方式2:那樣把定時器添加到NSRunLoopCommonModes模式下。

方式1和方式2在設置后都會在間隔設定的時間(本例中設置為2s)后執行test方法,如果需要立即執行可以使用下面的代碼。

[time fire];

關於NSTimer的開始,暫停,繼續,銷毀

-(IBAction)btnClick{
    [self starTimer];//開始計時
    //[self stopTimer];
}
 
-(NSTimer*)timer{
    if (!_timer) {
        _timer =[NSTimer scheduledTimerWithTimeInterval:5  target:self  selector:@selector(beginChange) userInfo:nil  repeats:YES];
    }
    return _timer;
}
 
//暫停定時器(只是暫停,並沒有銷毀timer)
-(void)pauseTimer{
    [self.timer setFireDate:[NSDate distantFuture]];
}
//繼續計時 -(void)continueTimer{ [self.timer setFireDate:[NSDate distantPast]]; } //開始計時 -(void)startimer{ [self.timer fire]; } //暫停並銷毀 -(void)stopTimer{ [self.timer invalidate]; self.timer = nil; } //開啟定時器 -(void)starTimer{ [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timerStar) object:nil]; [self performSelector:@selector(timerStar) withObject:nil afterDelay:1]; }

二, CADisplayLink

// 創建displayLink
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(test:)];
// 將創建的displaylink添加到runloop中,否則定時器不會執行
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

// 停止定時器
[displayLink invalidate];
 displayLink = nil;

當把CADisplayLink對象add到runloop中后,selector就能被周期性調用,類似於重復的NSTimer被啟動了;執行invalidate操作時,CADisplayLink對象就會從runloop中移除,selector調用也隨即停止,類似於NSTimer的invalidate方法

調用時機

CADisplayLink是一個和屏幕刷新率同步的定時器類。CADisplayLink以特定模式注冊到runloop后,每當屏幕顯示內容刷新結束的時候,runloop就會向CADisplayLink指定的target發送一次指定的selector消息,CADisplayLink類對應的selector就會被調用一次,所以可以使用CADisplayLink做一些和屏幕操作相關的操作。

重要屬性
  • frameInterval (時間戳)

    NSInteger類型的值,用來設置間隔多少幀調用一次selector方法,默認值是1,即每幀都調用一次。

  • duration (時間間隔)

    readOnly的CFTimeInterval值,表示兩次屏幕刷新之間的時間間隔。需要注意的是,該屬性在target的selector被首次調用以后才會被賦值。selector的調用間隔時間計算方式是:調用間隔時間 = duration × frameInterval。

  • isPaused (暫停/開啟) 

    isPaused設置為true時可以用於暫停通知
  • 修改幀率

    修改幀率: 如果在特定幀率內無法提供對象的操作,可以通過降低幀率解決.一個擁有持續穩定但是較慢幀率的應用要比跳幀的應用順滑的多.
    可以通過preferredFramesPerSecond來設置每秒刷新次數.preferredFramesPerSecond默認值為屏幕最大幀率(maximumFramesPerSecond),目前是60.
    實際的屏幕幀率會和preferredFramesPerSecond有一定的出入,結果是由設置的值和屏幕最大幀率(maximumFramesPerSecond)相互影響產生的.規則大概如下:

    如果屏幕最大幀率(preferredFramesPerSecond)是60,實際幀率只能是15, 20, 30, 60中的一種.如果設置大於60的值,屏幕實際幀率為60.如果設置的是26~35之間的值,實際幀率是

 注意:

        注意CADisplayLink是不能被繼承的.

三, GCD定時器

一次性定時
 dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC);
 dispatch_after(timer, dispatch_get_main_queue(), ^(void){
        NSLog(@"GCD-----%@",[NSThread currentThread]);
 });

 

重復執行的定時器
@property (nonatomic ,strong)dispatch_source_t timer;//  注意:此處應該使用強引用 strong
{
    //0.創建隊列
    dispatch_queue_t queue = dispatch_get_main_queue();
    //1.創建GCD中的定時器
    /*
     第一個參數:創建source的類型 DISPATCH_SOURCE_TYPE_TIMER:定時器
     第二個參數:0
     第三個參數:0
     第四個參數:隊列
     */
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);

    //2.設置時間等
    /*
     第一個參數:定時器對象
     第二個參數:DISPATCH_TIME_NOW 表示從現在開始計時
     第三個參數:間隔時間 GCD里面的時間最小單位為 納秒
     第四個參數:精准度(表示允許的誤差,0表示絕對精准)
     */
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);

    //3.要調用的任務
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"GCD-----%@",[NSThread currentThread]);
    });

    //4.開始執行
    dispatch_resume(timer);

    //
    self.timer = timer;
}
注意:
  • dispatch_source_t 在本質上是一個對象,所以我們使用強引用。我們可以點擊dispatch_source_t跳轉到source.h文件看到,改對象使用宏定義,進行了一系列操作定義對象。
    dispatch_source_t展開后的定義是:

@protocol OS_dispatch_source <OS_dispatch_object>
@end
typedef NSObject<OS_dispatch_source>* dispatch_source_t

   也就是dispatch_source_t 就是一個NSObjective對象。對於想深入了解的同學自行百度(主要是本人實在沒有那么多精力看源碼)

  • 定時器的暫停使用的是:dispatch_cancel(self.timer) 很明顯這個我們可以清楚得看到也是一個宏定義,至於他的內部操作,請參考上一句話
  • 相對於NSTimer來說GCD定時器更加精確,並且不用考慮運行模式,因為NSTimer其實是延遲把事件放到RunLoop中執行,如果遇到其他的事件導致NSTimer的事件執行錯過了運行周期,就會導致NSTimer的事件要到下一個周期才能運行。
  • 此處注意一定要強引用定時器 ,否則定時器執行到 } 后將會被釋放,無定時效果。
  • GCD定時器時間非常精准,最小的定時時間可以達到1納秒,所以用在非常精確的定時場合。


免責聲明!

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



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