iOS簡(jiǎn)單易用的GCD計(jì)時(shí)器的實(shí)現(xiàn)原理
前言
好久沒(méi)更新文章了,在掘金第一次發(fā)文章,還是給自己立一個(gè)flag每周至少更新一篇文章,可能文章的質(zhì)量還不是很如意,希望通過(guò)寫(xiě)文章來(lái)提高自己文筆,以及記錄自己學(xué)習(xí)中的遇到的問(wèn)題解決方案。
在學(xué)習(xí)iOS過(guò)程中,想定大家對(duì)于定時(shí)器都不陌生,在日常開(kāi)發(fā)中總會(huì)碰到需要計(jì)時(shí)器的功能,常見(jiàn)的定時(shí)器有NSTimer、GCD、CADisplayLink。網(wǎng)上也有很多的教程介紹三者的區(qū)別,今天主要講的是GCD這種方式使用以及封裝。
三者概括區(qū)別
優(yōu)點(diǎn) | 缺點(diǎn) | |
---|---|---|
NSTimer | 使用簡(jiǎn)單 | 受Runloop影響會(huì)導(dǎo)致計(jì)時(shí)不精準(zhǔn) |
CADisplayLink | 精度高 | CPU負(fù)載的時(shí)候會(huì)影響觸發(fā)事件,且觸發(fā)事件大于觸發(fā)間隔會(huì)導(dǎo)致掉幀現(xiàn)象。 |
GCD | 較精準(zhǔn) | 代碼較多,基本不受其他影響 |
總結(jié):NSTimer和CADisplayLink易受影響,而GCD雖然代碼多,但是可控性非常強(qiáng)。
GCD
/** 獲取一個(gè)全局的線程來(lái)運(yùn)行計(jì)時(shí)器*/ dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); /** 創(chuàng)建一個(gè)計(jì)時(shí)器*/ dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); /** 設(shè)置計(jì)時(shí)器, 這里是每10毫秒執(zhí)行一次*/ dispatch_source_set_timer(timer, dispatch_walltime(nil, 0), 10*NSEC_PER_MSEC, 0); /** 設(shè)置計(jì)時(shí)器的里操作事件*/ dispatch_source_set_event_handler(timer, ^{ //do you want.... });
開(kāi)啟、繼續(xù)已暫停的定時(shí)器
dispatch_resume(timer);
暫停定時(shí)器
/** 掛起的時(shí)候注意,多次暫停的操作會(huì)導(dǎo)致線程鎖的現(xiàn)象,即多少次暫停, * 對(duì)應(yīng)多少次的繼續(xù)操作,即dispatch_suspend和dispatch_resume * 是成對(duì)出現(xiàn)的,計(jì)時(shí)器才會(huì)繼續(xù)工作。 */ dispatch_suspend(timer);
結(jié)束定時(shí)器
dispatch_source_cancel(timer);
構(gòu)思封裝
寫(xiě)代碼之前構(gòu)思好功能模塊以及會(huì)遇到的問(wèn)題的解決方案、代碼邏輯,再來(lái)下手寫(xiě)代碼,會(huì)有事半功倍的效果。
- 必然包含開(kāi)始、暫停、繼續(xù)、停止、重置功能
- 時(shí)間計(jì)算過(guò)程中因浮點(diǎn)數(shù)計(jì)算會(huì)丟失精度,計(jì)算過(guò)程應(yīng)采用NSDecimal
- 時(shí)間轉(zhuǎn)換考慮到精度以及便利性,采用系統(tǒng)的時(shí)間轉(zhuǎn)換方法,時(shí)區(qū)置為GMT
- 由于APP進(jìn)入后臺(tái),若未開(kāi)啟后臺(tái)任務(wù)的開(kāi)關(guān),計(jì)時(shí)器將會(huì)停止,再次進(jìn)入APP又會(huì)繼續(xù),故采用監(jiān)聽(tīng)app狀態(tài)的方式記錄APP進(jìn)入后臺(tái)與前臺(tái)的時(shí)間戳,并與截止時(shí)間相比,是否繼續(xù)計(jì)時(shí)還是結(jié)束計(jì)時(shí)并回調(diào)。
- 計(jì)時(shí)器返回的結(jié)果若采用字符串則還需處理,故使用了一個(gè)時(shí)間類(lèi)來(lái)把結(jié)果返回,可以進(jìn)行自定義操作
- 倒計(jì)時(shí)的結(jié)果返回和結(jié)束通知采用閉包形式
部分代碼
/** app進(jìn)入后臺(tái)*/ - (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進(jìn)入前臺(tái)*/ - (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]; } /** 不失精度加減乘除計(jì)算結(jié)果*/ - (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的版本的話可以留言或者私我,可以在寫(xiě)個(gè)Swift版本,:stuck_out_tongue_winking_eye:。
結(jié)語(yǔ)
使用簡(jiǎn)單,只需要把OMTimer.h和OMTimer.m拖入你的工程即可,滿(mǎn)足大數(shù)的場(chǎng)景,可倒計(jì)時(shí)亦可增加計(jì)時(shí),全部代碼已在Github<https://github.com/oymuzi/OMKit/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)百度外賣(mài)頭像波浪的效果
對(duì)于現(xiàn)在很多人來(lái)說(shuō),叫外賣(mài)就成了不可或缺的習(xí)慣。某日瞬間發(fā)現(xiàn)百度外賣(mài)的APP波浪效果很是吸引人,相比較其他的外賣(mài)APP,顏值略高些.(淘寶也有波浪的效果),遂就思考如何實(shí)現(xiàn)這種"浪"的效果,下面來(lái)一起看看。2016-08-08

iOS中實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪圖片功能實(shí)例

iOS中詳解Block作為property屬性實(shí)現(xiàn)頁(yè)面之間傳值

iOS中使用UIDatePicker制作時(shí)間選擇器的實(shí)例教程

iOS判斷運(yùn)營(yíng)商類(lèi)型的實(shí)現(xiàn)方法

iOS應(yīng)用開(kāi)發(fā)中StoryBoard搭建UI界面的基本使用講解