验证码倒计时和时间倒计时
在项目开发过程中都会有倒计时的内容,今天自己弄的GCD写的倒计时
1> UIButton 点击 倒计时60 秒; 粘贴到工程即可
UIButton的扩展
.h文件
#import@interface UIButton (Extension)
/**
获得倒计时的事件差
@param sumTimes 倒计时的总时间
@param originalTitle 显示的原始信息数据
*/
- (void)startSumTimes:(NSInteger) sumTimes originalTitle:(NSString *) originalTitle;
@end
.m文件
#import "UIButton+Extension.h"
@implementation UIButton (Extension)
/**
获得倒计时的事件差
@param sumTimes 倒计时的总时间
@param originalTitle 显示的原始信息数据
*/
- (void)startSumTimes:(NSInteger) sumTimes originalTitle:(NSString *) originalTitle {
//关闭用户交互
self.userInteractionEnabled = NO;
// 倒计时时间
__block NSInteger timeOut = sumTimes;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _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);
dispatch_async(dispatch_get_main_queue(), ^{
[self setTitle:originalTitle forState:UIControlStateNormal];
self.userInteractionEnabled = YES;
});
}
else
{
NSString * timeStr = [NSString stringWithFormat:@"%0.2ld",timeOut];
dispatch_async(dispatch_get_main_queue(), ^{
[self setTitle:[NSString stringWithFormat:@"%@%@",timeStr,@"s后获取"] forState:UIControlStateNormal];
self.userInteractionEnabled = NO;
});
timeOut --;
}
});
dispatch_resume(_timer);
}
@end
创建UIButton 代码如下:
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor=[UIColor grayColor];
[button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview: button];
//masonry适配
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(kTopHeight);
make.left.mas_equalTo(20);
make.size.mas_equalTo(CGSizeMake(200, 40));
}];
//点击方法
-(void)onButtonClick:(UIButton *)sender{
// 倒计时实现
[sender startSumTimes:60 originalTitle:@"获取验证码"];
}
效果视图2> 时间倒计时, 与上事例一样, UILable的扩展代码如下:
.h文件
#import@interface UILabel (Extension)
- (void)startSumTimes:(NSString *)overTimes;
@end
.m文件
#import "UILabel+Extension.h"
@implementation UILabel (Extension)
- (void)startSumTimes:(NSString *)overTimes
{
//overTimes 时间字符串
FMStringToDatdeTool *dateTool=[[FMStringToDatdeTool alloc]init];
NSDate *overTime=[dateTool stringIsChangeDadeWith:overTimes];//结束时间
NSDate *nowDate=[NSDate dateWithTimeIntervalSinceNow:8*3600];
//这里 你们可以自己计算倒计时的时间,小编这里是 当前时间和2018-11-10 19:00:00来设置的
NSTimeInterval seconds = [overTime timeIntervalSinceDate:nowDate]; //两个时间的差值 秒为单位
// 倒计时时间
__block NSInteger timeOut = seconds;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _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);
dispatch_async(dispatch_get_main_queue(), ^{
self.text =@"倒计时完成";
});
}
else
{
//计算显示的时间
NSString *str_day = [NSString stringWithFormat:@"%02ld",timeOut/(3600*24)];
NSString *str_hour = [NSString stringWithFormat:@"%02ld",(timeOut%(3600*24))/3600];
NSString *str_minute = [NSString stringWithFormat:@"%02ld",(timeOut%3600)/60];
NSString *str_second = [NSString stringWithFormat:@"%02ld",timeOut%60];
NSString *format_time = [NSString stringWithFormat:@"%@天%@小时%@分%@秒",str_day,str_hour,str_minute,str_second];
//修改倒计时标签现实内容
dispatch_async(dispatch_get_main_queue(), ^{
self.text=[NSString stringWithFormat:@"倒计时时间 %@",format_time];
});
timeOut --;
}
});
dispatch_resume(_timer);
}
创建UILable 进行显示,
[self.timeLabel startSumTimes:@"2018-11-10 19:00:00"];
效果演示后续Dome 待上传