一、NSTimer(一般用于定時(shí)的更新一些非界面上的數(shù)據(jù))
1. 創(chuàng)建方法
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(action:) userInfo:nil repeats:NO];
TimerInterval : 執(zhí)行之前等待的時(shí)間。比如設(shè)置成1.0噪沙,就代表1秒后執(zhí)行方法
target : 需要執(zhí)行方法的對象。
selector : 需要執(zhí)行的方法
repeats : 是否需要循環(huán)
2. 釋放方法
[timer invalidate];
注意 :
調(diào)用創(chuàng)建方法后篓像,target對象的計(jì)數(shù)器會加1,直到執(zhí)行完畢,自動(dòng)減1。如果是循環(huán)執(zhí)行的話急鳄,就必須手動(dòng)關(guān)閉,否則可以不執(zhí)行釋放方法嚷闭。
3. 特性
存在延遲
不管是一次性的還是周期性的timer的實(shí)際觸發(fā)事件的時(shí)間攒岛,都會與所加入的RunLoop和RunLoop Mode有關(guān)赖临,如果此RunLoop正在執(zhí)行一個(gè)連續(xù)性的運(yùn)算胞锰,timer就會被延時(shí)出發(fā)。重復(fù)性的timer遇到這種情況兢榨,如果延遲超過了一個(gè)周期嗅榕,則會在延時(shí)結(jié)束后立刻執(zhí)行,并按照之前指定的周期繼續(xù)執(zhí)行吵聪。
必須加入Runloop
使用上面的創(chuàng)建方式凌那,會自動(dòng)把timer加入MainRunloop的NSDefaultRunLoopMode中。如果使用以下方式創(chuàng)建定時(shí)器吟逝,就必須手動(dòng)加入Runloop:
NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
二帽蝶、CADisplayLink
1. 創(chuàng)建方法
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)];
[self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
2. 停止方法
self.displayLink invalidate];
self.displayLink = nil;
當(dāng)把CADisplayLink對象add到runloop中后,selector就能被周期性調(diào)用块攒,類似于重復(fù)的NSTimer被啟動(dòng)了励稳;執(zhí)行invalidate操作時(shí),CADisplayLink對象就會從runloop中移除囱井,selector調(diào)用也隨即停止驹尼,類似于NSTimer的invalidate方法。
3. 特性
屏幕刷新時(shí)調(diào)用
CADisplayLink是一個(gè)能讓我們以和屏幕刷新率同步的頻率將特定的內(nèi)容畫到屏幕上的定時(shí)器類庞呕。CADisplayLink以特定模式注冊到runloop后新翎,每當(dāng)屏幕顯示內(nèi)容刷新結(jié)束的時(shí)候,runloop就會向CADisplayLink指定的target發(fā)送一次指定的selector消息住练, CADisplayLink類對應(yīng)的selector就會被調(diào)用一次地啰。所以通常情況下,按照iOS設(shè)備屏幕的刷新率60次/秒
延遲
iOS設(shè)備的屏幕刷新頻率是固定的讲逛,CADisplayLink在正常情況下會在每次刷新結(jié)束都被調(diào)用亏吝,精確度相當(dāng)高。但如果調(diào)用的方法比較耗時(shí)妆绞,超過了屏幕刷新周期顺呕,就會導(dǎo)致跳過若干次回調(diào)調(diào)用機(jī)會枫攀。
如果CPU過于繁忙,無法保證屏幕60次/秒的刷新率株茶,就會導(dǎo)致跳過若干次調(diào)用回調(diào)方法的機(jī)會来涨,跳過次數(shù)取決CPU的忙碌程度。
使用場景
從原理上可以看出启盛,CADisplayLink適合做界面的不停重繪蹦掐,比如視頻播放的時(shí)候需要不停地獲取下一幀用于界面渲染。
4. 重要屬性
frameInterval
NSInteger類型的值僵闯,用來設(shè)置間隔多少幀調(diào)用一次selector方法卧抗,默認(rèn)值是1,即每幀都調(diào)用一次鳖粟。
duration
readOnly的CFTimeInterval值社裆,表示兩次屏幕刷新之間的時(shí)間間隔。需要注意的是向图,該屬性在target的selector被首次調(diào)用以后才會被賦值泳秀。selector的調(diào)用間隔時(shí)間計(jì)算方式是:調(diào)用間隔時(shí)間 = 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è)置時(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);