轉(zhuǎn)自: iOS中幾種定時器 - 控制了時間炕泳,就控制了一切
定時器三種方法:
- NSTimer
- CADisplayLink
- GCD方式
一、NSTimer
1.1 基本使用方法
1.1.1 創(chuàng)建
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(action:) userInfo:nil repeats:NO];
- TimerInterval : 執(zhí)行之前等待的時間搂抒。比如設(shè)置成1.0,就代表1秒后執(zhí)行方法
- target : 需要執(zhí)行方法的對象兔沃。
- selector : 需要執(zhí)行的方法
- repeats : 是否需要循環(huán)
1.1.2 開始/停止/釋放
開始:
[timer setFireDate:[NSDate distantPast]];
停止:
[timer setFireDate:[NSDate distantFuture]];
釋放:
[timer invalidate];
注意 :
調(diào)用創(chuàng)建方法后狡逢,target對象的計數(shù)器會加1,直到執(zhí)行完畢棚亩,自動減1蓖议。如果是循環(huán)執(zhí)行的話虏杰,就必須手動關(guān)閉,否則可以不執(zhí)行釋放方法勒虾。
1.2 特性
存在延遲
不管是一次性的還是周期性的timer的實際觸發(fā)事件的時間纺阔,都會與所加入的RunLoop和RunLoop Mode有關(guān),如果此RunLoop正在執(zhí)行一個連續(xù)性的運算修然,timer就會被延時出發(fā)笛钝。重復(fù)性的timer遇到這種情況,如果延遲超過了一個周期愕宋,則會在延時結(jié)束后立刻執(zhí)行玻靡,并按照之前指定的周期繼續(xù)執(zhí)行。必須加入Runloop
使用上面的創(chuàng)建方式中贝,會自動把timer加入MainRunloop的NSDefaultRunLoopMode中囤捻。如果使用以下方式創(chuàng)建定時器,就必須手動加入Runloop:
NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
二邻寿、CADisplayLink
2.1. 創(chuàng)建方法
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
2.2 停止方法
[self.displayLink invalidate];
self.displayLink = nil;
2.3 特性
- 屏幕刷新時調(diào)用
CADisplayLink是一個能讓我們以和屏幕刷新率同步的頻率將特定的內(nèi)容畫到屏幕上的定時器類。CADisplayLink以特定模式注冊到runloop后淀弹,每當屏幕顯示內(nèi)容刷新結(jié)束的時候,runloop就會向CADisplayLink指定的target發(fā)送一次指定的selector消息庆械, CADisplayLink類對應(yīng)的selector就會被調(diào)用一次薇溃。所以通常情況下,按照iOS設(shè)備屏幕的刷新率60次/秒 - 延遲
iOS設(shè)備的屏幕刷新頻率是固定的缭乘,CADisplayLink在正常情況下會在每次刷新結(jié)束都被調(diào)用沐序,精確度相當高。但如果調(diào)用的方法比較耗時堕绩,超過了屏幕刷新周期策幼,就會導(dǎo)致跳過若干次回調(diào)調(diào)用機會。
如果CPU過于繁忙奴紧,無法保證屏幕60次/秒的刷新率特姐,就會導(dǎo)致跳過若干次調(diào)用回調(diào)方法的機會,跳過次數(shù)取決CPU的忙碌程度黍氮。 - 使用場景
從原理上可以看出唐含,CADisplayLink適合做界面的不停重繪浅浮,比如視頻播放的時候需要不停地獲取下一幀用于界面渲染。
2.4 重要屬性
- frameInterval
NSInteger類型的值捷枯,用來設(shè)置間隔多少幀調(diào)用一次selector方法滚秩,默認值是1,即每幀都調(diào)用一次淮捆。 - duration
readOnly的CFTimeInterval值郁油,表示兩次屏幕刷新之間的時間間隔。需要注意的是攀痊,該屬性在target的selector被首次調(diào)用以后才會被賦值已艰。selector的調(diào)用間隔時間計算方式是:調(diào)用間隔時間 = duration × frameInterval。
三蚕苇、GCD方式
- 執(zhí)行一次
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//執(zhí)行事件
});
- 重復(fù)執(zhí)行
NSTimeInterval period = 1.0; //設(shè)置時間間隔
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒執(zhí)行
dispatch_source_set_event_handler(_timer, ^{
//在這里執(zhí)行事件
});
dispatch_resume(_timer);
GCD的方式哩掺,我在網(wǎng)上只能找到這些資料,目前自己還在學(xué)習(xí)中涩笤,以后會更新