IOS 時間定時器

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

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

第一種方法創建的定時器會自帶RunLoop 但是在滑動屏幕是RunLoop會進入另外一種模式 定時器會暫停 可以向第二種定時器一樣把定時器添加到NSRunLoopCommonModes 模式下 如果需要立即執行 我們可以執行 [time fire]; 方法就可以立即執行

還有之中是GCD定時器
還有一種是CADisplayLink 定時器

發佈了51 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章