iOS簡單易用的GCD計時器的實現(xiàn)原理
前言
好久沒更新文章了,在掘金第一次發(fā)文章,還是給自己立一個flag每周至少更新一篇文章,可能文章的質量還不是很如意,希望通過寫文章來提高自己文筆,以及記錄自己學習中的遇到的問題解決方案。
在學習iOS過程中,想定大家對于定時器都不陌生,在日常開發(fā)中總會碰到需要計時器的功能,常見的定時器有NSTimer、GCD、CADisplayLink。網(wǎng)上也有很多的教程介紹三者的區(qū)別,今天主要講的是GCD這種方式使用以及封裝。
三者概括區(qū)別
優(yōu)點 | 缺點 | |
---|---|---|
NSTimer | 使用簡單 | 受Runloop影響會導致計時不精準 |
CADisplayLink | 精度高 | CPU負載的時候會影響觸發(fā)事件,且觸發(fā)事件大于觸發(fā)間隔會導致掉幀現(xiàn)象。 |
GCD | 較精準 | 代碼較多,基本不受其他影響 |
總結:NSTimer和CADisplayLink易受影響,而GCD雖然代碼多,但是可控性非常強。
GCD
/** 獲取一個全局的線程來運行計時器*/ dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); /** 創(chuàng)建一個計時器*/ dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); /** 設置計時器, 這里是每10毫秒執(zhí)行一次*/ dispatch_source_set_timer(timer, dispatch_walltime(nil, 0), 10*NSEC_PER_MSEC, 0); /** 設置計時器的里操作事件*/ dispatch_source_set_event_handler(timer, ^{ //do you want.... });
開啟、繼續(xù)已暫停的定時器
dispatch_resume(timer);
暫停定時器
/** 掛起的時候注意,多次暫停的操作會導致線程鎖的現(xiàn)象,即多少次暫停, * 對應多少次的繼續(xù)操作,即dispatch_suspend和dispatch_resume * 是成對出現(xiàn)的,計時器才會繼續(xù)工作。 */ dispatch_suspend(timer);
結束定時器
dispatch_source_cancel(timer);
構思封裝
寫代碼之前構思好功能模塊以及會遇到的問題的解決方案、代碼邏輯,再來下手寫代碼,會有事半功倍的效果。
- 必然包含開始、暫停、繼續(xù)、停止、重置功能
- 時間計算過程中因浮點數(shù)計算會丟失精度,計算過程應采用NSDecimal
- 時間轉換考慮到精度以及便利性,采用系統(tǒng)的時間轉換方法,時區(qū)置為GMT
- 由于APP進入后臺,若未開啟后臺任務的開關,計時器將會停止,再次進入APP又會繼續(xù),故采用監(jiān)聽app狀態(tài)的方式記錄APP進入后臺與前臺的時間戳,并與截止時間相比,是否繼續(xù)計時還是結束計時并回調。
- 計時器返回的結果若采用字符串則還需處理,故使用了一個時間類來把結果返回,可以進行自定義操作
- 倒計時的結果返回和結束通知采用閉包形式
部分代碼
/** app進入后臺*/ - (void)appDidEnterBackground{ [self suspend]; NSDate *date = [[NSDate alloc] init]; NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd HH:mm:ss:SSS"; self.appDidEnterBackgroundTime = [date timeIntervalSince1970]; } /** app進入前臺*/ - (void)appDidEnterForeground{ NSDate *date = [[NSDate alloc] init]; NSDateFormatter *format = [[NSDateFormatter alloc] init]; format.dateFormat = @"yyyy-MM-dd HH:mm:ss"; self.appDidEnterForegroundTime = [date timeIntervalSince1970]; [self reCalculateMinder]; } /** 不失精度加減乘除計算結果*/ - (NSDecimalNumber *)value: (NSTimeInterval)value byOpration: (OMDecimalOprationType)byOpration percision: (NSInteger)percision withValue: (NSTimeInterval)withValue{ NSDecimalNumber *number = [self numberValueWithString: value]; NSDecimalNumber *withNumber = [self numberValueWithString: withValue]; NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode: NSRoundPlain scale: percision raiseOnExactness: NO raiseOnOverflow: NO raiseOnUnderflow: NO raiseOnDivideByZero: YES]; switch (byOpration) { case OMDecimalOprationTypeAdd: return [number decimalNumberByAdding: withNumber withBehavior:handler]; break; case OMDecimalOprationTypeSubtract: return [number decimalNumberBySubtracting: withNumber withBehavior: handler]; break; case OMDecimalOprationTypeDivide: return [number decimalNumberByDividingBy: withNumber withBehavior: handler]; break; case OMDecimalOprationTypeMultiple: return [number decimalNumberByMultiplyingBy: withNumber withBehavior: handler]; break; default: break; return nil; }
@property (nonatomic, strong) OMTimer *timer;
self.timer = [[OMTimer alloc] init]; self.timer.timerInterval = 30; self.timer.precision = 100; self.timer.isAscend = NO; self.timer.progressBlock = ^(OMTime *progress) { NSLog(@"%@:%@:%@:%@", progress.hour, progress.minute, progress.second, progress.millisecond; };self.timer.completion = ^{ NSLog(@"complete done!"); };
Swift版本
最近喜歡上了OC,如有小伙伴需要Swift的版本的話可以留言或者私我,可以在寫個Swift版本,:stuck_out_tongue_winking_eye:。
結語
使用簡單,只需要把OMTimer.h和OMTimer.m拖入你的工程即可,滿足大數(shù)的場景,可倒計時亦可增加計時,全部代碼已在Github<https://github.com/oymuzi/OMKit/
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS中實現(xiàn)動態(tài)區(qū)域裁剪圖片功能實例
圖片處理中經常用的圖片剪裁,就是通過剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,下面這篇文章主要給大家介紹了關于iOS中實現(xiàn)動態(tài)區(qū)域裁剪圖片功能的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。2017-11-11iOS中詳解Block作為property屬性實現(xiàn)頁面之間傳值
這篇文章主要介紹了iOS中Block作為property屬性實現(xiàn)頁面之間傳值的相關知識點,以及代碼分享,一起學習下吧。2018-02-02iOS中使用UIDatePicker制作時間選擇器的實例教程
這篇文章主要介紹了iOS中使用UIDatePicker制作時間選擇器的實例教程,實例中未選中的時間項目會講解一個將其變透明的方法,非常給力,需要的朋友可以參考下2016-05-05iOS應用開發(fā)中StoryBoard搭建UI界面的基本使用講解
這篇文章主要介紹了iOS應用開發(fā)中StoryBoard搭建UI界面的基本使用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-02-02