iOS中封裝常見的倒計時(獲取驗證碼、支付倒計時、商城倒計時)

? ? ? ? ?做一朵白蓮花吧,在物欲橫流的社會底層靜靜發育,不沾染一點灰塵,慢慢伸出理想的枝干,穿越爾虞我詐的茂密荷葉,在瀲滟的水面上開出潔白的花瓣,笑對陽光,笑對夢雨,笑對秋風,笑對看客,然后結成一個純凈的心靈蓮蓬,在里面珍藏潔白如玉的理想蓮子。 ?

? ? ? ? ?之所以,寫這篇文章,是本菜鳥覺得獲取驗證碼倒計時這個知識點貌似、大概、也許,每個App基本上都會遇到的,為了不再寫那么都煩躁的代碼,所有上來與大家共勉一下,希望大家多多指教。又因為最近項目遇到支付倒計時,所有也把這個方法做了修改一起分享出來。

------------------------------使用方法------------------------------------

一:最常見的:注冊、忘記密碼的時候獲取驗證碼倒計時

看起來是不是很爽,代碼分離出來,注冊、忘記密碼等等獲取驗證碼只要幾行代碼就解決了

二:支付過程支付時間倒計時、商城活動倒計時



------------------------------.h------------------------------------

#import

@interfaceCountDown :NSObject

///支付過程倒計時商城倒計時

-(void)countDownWithSeparate:(NSInteger)totalTime completeBlock:(void(^)(NSIntegerday,NSIntegerhour,NSIntegerminute,NSIntegersecond))completeBlock;

///獲取驗證碼倒計時

-(void)countDownWithTime:(NSInteger)totalTime completeBlock:(void(^)(NSIntegercountDown))completeBlock;

///主動銷毀定時器

-(void)destoryTimer;

@end


------------------------------.m------------------------------------

#import"CountDown.h"

@interfaceCountDown()

@property(nonatomic,retain)dispatch_source_ttimer;

@end

@implementationCountDown

#pragma mark支付過程倒計時 ? ?商城倒計時

-(void)countDownWithSeparate:(NSInteger)totalTime completeBlock:(void(^)(NSIntegerday,NSIntegerhour,NSIntegerminute,NSIntegersecond))completeBlock{

if(_timer==nil) {

__blockNSIntegertimeout = totalTime;

if(timeout !=0) {

dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

_timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,queue);

dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0);

dispatch_source_set_event_handler(_timer, ^{

if(timeout <=0){

dispatch_source_cancel(_timer);

_timer=nil;

dispatch_async(dispatch_get_main_queue(), ^{

completeBlock(0,0,0,0);

});

}else{

NSIntegertotalTime = timeout;

NSIntegers =1;

NSIntegerm = s *60;

NSIntegerh = m *60;

NSIntegerd = h *24;

NSIntegerday = totalTime / d;//天

NSIntegerhour = (totalTime - day * d) / h;//時考慮天1天23小時59分鐘59秒

//NSInteger onlyhour = totalTime / h;//時不考慮天只考慮到小時28小時59分鐘59秒

NSIntegerminute = (totalTime - day * d - hour * h) / m;//分

NSIntegersecond = (totalTime - day * d - hour * h - minute * m) / s;//秒

dispatch_async(dispatch_get_main_queue(), ^{

completeBlock(day,hour,minute,second);

});

//不考慮天的回調28小時59分鐘59秒

//dispatch_async(dispatch_get_main_queue(), ^{

//completeBlock(0,onlyhour,minute,second);

//});

timeout--;

}

});

dispatch_resume(_timer);

}

}

}

#pragma mark獲取驗證碼倒計時

-(void)countDownWithTime:(NSInteger)totalTime completeBlock:(void(^)(NSIntegercountDown))completeBlock

{

if(_timer==nil) {

__blockNSIntegertimeout = totalTime;

if(timeout !=0) {

dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

_timer=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,queue);

dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0);

dispatch_source_set_event_handler(_timer, ^{

if(timeout <=0){

dispatch_source_cancel(_timer);

_timer=nil;

dispatch_async(dispatch_get_main_queue(), ^{

completeBlock(0);

});

}else{

dispatch_async(dispatch_get_main_queue(), ^{

completeBlock(timeout);

});

timeout--;

}

});

dispatch_resume(_timer);

}

}

}

#pragma mark主動銷毀定時器

-(void)destoryTimer

{

if(_timer){

dispatch_source_cancel(_timer);

_timer=nil;

}

}

@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容