做一朵白莲花吧,在物欲横流的社会底层静静发育,不沾染一点灰尘,慢慢伸出理想的枝干,穿越尔虞我诈的茂密荷叶,在潋滟的水面上开出洁白的花瓣,笑对阳光,笑对梦雨,笑对秋风,笑对看客,然后结成一个纯净的心灵莲蓬,在里面珍藏洁白如玉的理想莲子。
之所以,写这篇文章,是本菜鸟觉得获取验证码倒计时这个知识点貌似、大概、也许,每个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