一、NSTimer
- (instancetype)initWithFireDate:(NSDate *)date
interval:(NSTimeInterval)ti
target:(id)t
selector:(SEL)s
userInfo:(nullable id)ui
repeats:(BOOL)rep
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti
invocation:(NSInvocation *)invocation
repeats:(BOOL)yesOrNo;
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti
target:(id)aTarget
selector:(SEL)aSelector
userInfo:(nullable id)userInfo
repeats:(BOOL)yesOrNo;
TimerInterval: 執(zhí)行之前等待的時間。比如設置成1.0,就代表1秒后執(zhí)行方法
target: 需要執(zhí)行方法的對象。
selector : 需要執(zhí)行的方法
repeats : 是否需要循環(huán)
以上的方法用來創(chuàng)建定時器,但是創(chuàng)建完必須自己把其添加到RunLoop
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
invocation:(NSInvocation *)invocation
repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
target:(id)aTarget
selector:(SEL)aSelector
userInfo:(nullable id)userInfo
repeats:(BOOL)yesOrNo;
以上兩個方法也是創(chuàng)建定時器的方法,但在創(chuàng)建的同時內部已經把定時器添加到主循環(huán)中,并且是RunLoop默認模式,因此會被滑動等時間影響。
例如:
//初始化一個Invocation對象
NSInvocation * invo = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(init)]];
[invo setTarget:self];
[invo setSelector:@selector(timerAction)];
NSTimer * timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];
//加入主循環(huán)池中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
//開始循環(huán)
[timer fire];
//創(chuàng)建定時器
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//添加到主循環(huán)
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
釋放定時器:
[timer invalidate];
timer = nil;
用NSTimer來作為定時器會存在延遲的缺點。因為NSTimer也是做為一種資源添加到RunLoop中,所以其觸發(fā)的時間等都與RunLoop相關。如果RunLoop正在進行連續(xù)性的計算,會延時觸發(fā)NSTimer。
在OS X v10.9以后為了盡量避免在NSTimer觸發(fā)時間到了而去中斷當前處理的任務,NSTimer新增了tolerance屬性,讓用戶可以設置可以容忍的觸發(fā)的時間范圍。
二、CADisplayLink
//創(chuàng)建CADisplayLink
CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(MyAction)];
//添加到主循環(huán)
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
//結束一個CADisplayLink
[displayLink invalidate];
CADisplayLink創(chuàng)建后也需要添加到RunLoop
屬性:
paused : 設置這個屬性可以控制CADisplayLink是否暫停
duration : 提供了每幀之間的時間,也就是屏幕每次刷新之間的的時間(為只讀屬性)
frameInterval :調用的幀間隔, 默認值為1,屏幕每刷新一幀就調用,即1/60秒一次。如果設置為2,就會兩幀調用一次,即1/30秒。
- CADisplayLink這個定時器的頻率和屏幕刷新率相同,因此適合在不停重繪界面的場合使用,比如自定義動畫引擎或者視頻播放的渲染。
- iOS設備的屏幕刷新頻率是固定的,CADisplayLink在正常情況下會在每次刷新結束都被調用,精確度相當高。但如果調用的方法比較耗時,超過了屏幕刷新周期,就會導致跳過若干次回調調用機會。
如果CPU過于繁忙,無法保證屏幕60次/秒的刷新率,就會導致跳過若干次調用回調方法的機會,跳過次數(shù)取決CPU的忙碌程度。
三、dispatch_source
@property (nonatomic, strong) dispatch_source_t timer;
__block NSInteger count = 0;
//創(chuàng)建隊列
dispatch_queue_t queue = dispatch_get_main_queue();
//創(chuàng)建定時器
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//設置定時器時間
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, 0);
uint64_t interval = (uint64_t)(1.0 * NSEC_PER_SEC);
dispatch_source_set_timer(self.timer, start, interval, 0);
//設置回調
dispatch_source_set_event_handler(self.timer, ^{
//重復執(zhí)行的事件
NSLog(@"-----%ld-----", count);
count++;
if (count == 5) {
//停止定時器
dispatch_source_cancel(self.timer);
self.timer = nil;
}
});
//啟動定時器
dispatch_resume(self.timer);
dispatch_source創(chuàng)建的定時器精確度最高,并且還可以放在子線程中,解決定時間跑在主線程上卡UI問題