? ? ? ? ?做一朵白蓮花吧,在物欲橫流的社會(huì)底層靜靜發(fā)育妇穴,不沾染一點(diǎn)灰塵爬虱,慢慢伸出理想的枝干隶债,穿越爾虞我詐的茂密荷葉,在瀲滟的水面上開(kāi)出潔白的花瓣跑筝,笑對(duì)陽(yáng)光死讹,笑對(duì)夢(mèng)雨,笑對(duì)秋風(fēng)曲梗,笑對(duì)看客赞警,然后結(jié)成一個(gè)純凈的心靈蓮蓬,在里面珍藏潔白如玉的理想蓮子虏两。 ?
? ? ? ? ?之所以愧旦,寫(xiě)這篇文章,是本菜鳥(niǎo)覺(jué)得獲取驗(yàn)證碼倒計(jì)時(shí)這個(gè)知識(shí)點(diǎn)貌似定罢、大概笤虫、也許,每個(gè)App基本上都會(huì)遇到的祖凫,為了不再寫(xiě)那么都煩躁的代碼琼蚯,所有上來(lái)與大家共勉一下,希望大家多多指教蝙场。又因?yàn)樽罱?xiàng)目遇到支付倒計(jì)時(shí)凌停,所有也把這個(gè)方法做了修改一起分享出來(lái)。
------------------------------使用方法------------------------------------
一:最常見(jiàn)的:注冊(cè)售滤、忘記密碼的時(shí)候獲取驗(yàn)證碼倒計(jì)時(shí)
二:支付過(guò)程支付時(shí)間倒計(jì)時(shí)赐俗、商城活動(dòng)倒計(jì)時(shí)
------------------------------.h------------------------------------
#import
@interfaceCountDown :NSObject
///支付過(guò)程倒計(jì)時(shí)商城倒計(jì)時(shí)
-(void)countDownWithSeparate:(NSInteger)totalTime completeBlock:(void(^)(NSIntegerday,NSIntegerhour,NSIntegerminute,NSIntegersecond))completeBlock;
///獲取驗(yàn)證碼倒計(jì)時(shí)
-(void)countDownWithTime:(NSInteger)totalTime completeBlock:(void(^)(NSIntegercountDown))completeBlock;
///主動(dòng)銷(xiāo)毀定時(shí)器
-(void)destoryTimer;
@end
------------------------------.m------------------------------------
#import"CountDown.h"
@interfaceCountDown()
@property(nonatomic,retain)dispatch_source_ttimer;
@end
@implementationCountDown
#pragma mark支付過(guò)程倒計(jì)時(shí) ? ?商城倒計(jì)時(shí)
-(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;//時(shí)考慮天1天23小時(shí)59分鐘59秒
//NSInteger onlyhour = totalTime / h;//時(shí)不考慮天只考慮到小時(shí)28小時(shí)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);
});
//不考慮天的回調(diào)28小時(shí)59分鐘59秒
//dispatch_async(dispatch_get_main_queue(), ^{
//completeBlock(0,onlyhour,minute,second);
//});
timeout--;
}
});
dispatch_resume(_timer);
}
}
}
#pragma mark獲取驗(yàn)證碼倒計(jì)時(shí)
-(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主動(dòng)銷(xiāo)毀定時(shí)器
-(void)destoryTimer
{
if(_timer){
dispatch_source_cancel(_timer);
_timer=nil;
}
}
@end