在項(xiàng)目開(kāi)發(fā)過(guò)程中都會(huì)有倒計(jì)時(shí)的內(nèi)容,今天自己弄的GCD寫(xiě)的倒計(jì)時(shí)
1> UIButton 點(diǎn)擊 倒計(jì)時(shí)60 秒; 粘貼到工程即可
UIButton的擴(kuò)展
.h文件
#import@interface UIButton (Extension)
/**
獲得倒計(jì)時(shí)的事件差
@param sumTimes 倒計(jì)時(shí)的總時(shí)間
@param originalTitle 顯示的原始信息數(shù)據(jù)
*/
- (void)startSumTimes:(NSInteger) sumTimes originalTitle:(NSString *) originalTitle;
@end
.m文件
#import "UIButton+Extension.h"
@implementation UIButton (Extension)
/**
獲得倒計(jì)時(shí)的事件差
@param sumTimes 倒計(jì)時(shí)的總時(shí)間
@param originalTitle 顯示的原始信息數(shù)據(jù)
*/
- (void)startSumTimes:(NSInteger) sumTimes originalTitle:(NSString *) originalTitle {
? ? //關(guān)閉用戶交互
? ? self.userInteractionEnabled = NO;
? ? // 倒計(jì)時(shí)時(shí)間
? ? __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);
? ? // 每秒執(zhí)行一次
? ? dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
? ? dispatch_source_set_event_handler(_timer, ^{
? ? ? ? // 倒計(jì)時(shí)結(jié)束徙融,關(guān)閉
? ? ? ? if (timeOut <= 0)
? ? ? ? {
? ? ? ? ? ? // 打開(kāi)用戶交互
? ? ? ? ? ? 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
創(chuàng)建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));
? ? }];
//點(diǎn)擊方法
-(void)onButtonClick:(UIButton *)sender{
// 倒計(jì)時(shí)實(shí)現(xiàn)
?? ?[sender startSumTimes:60 originalTitle:@"獲取驗(yàn)證碼"];
}
2> 時(shí)間倒計(jì)時(shí), 與上事例一樣, UILable的擴(kuò)展代碼如下:
.h文件
#import@interface UILabel (Extension)
- (void)startSumTimes:(NSString *)overTimes;
@end
.m文件
#import "UILabel+Extension.h"
@implementation UILabel (Extension)
- (void)startSumTimes:(NSString *)overTimes?
{
//overTimes 時(shí)間字符串
? ? FMStringToDatdeTool *dateTool=[[FMStringToDatdeTool alloc]init];
? ?NSDate *overTime=[dateTool stringIsChangeDadeWith:overTimes];//結(jié)束時(shí)間
? ? NSDate *nowDate=[NSDate dateWithTimeIntervalSinceNow:8*3600];
//這里 你們可以自己計(jì)算倒計(jì)時(shí)的時(shí)間,小編這里是 當(dāng)前時(shí)間和2018-11-10 19:00:00來(lái)設(shè)置的
? ? NSTimeInterval seconds = [overTime timeIntervalSinceDate:nowDate]; //兩個(gè)時(shí)間的差值 秒為單位
? ? // 倒計(jì)時(shí)時(shí)間
? ? __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);
? ? // 每秒執(zhí)行一次
? ? dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
? ? dispatch_source_set_event_handler(_timer, ^{
? ? ? ? // 倒計(jì)時(shí)結(jié)束洒缀,關(guān)閉
? ? ? ? if (timeOut <= 0) {
? ? ? ? ? ? // 打開(kāi)用戶交互
? ? ? ? ? ? dispatch_source_cancel(_timer);
? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ?self.text =@"倒計(jì)時(shí)完成";
? ? ? ? ? ? });
? ? ? ? }
? ? ? ? else
? ? ? ? {
?//計(jì)算顯示的時(shí)間
? ? ? ? ? ? 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:@"%@天%@小時(shí)%@分%@秒",str_day,str_hour,str_minute,str_second];
? ? ? ? ? ? //修改倒計(jì)時(shí)標(biāo)簽現(xiàn)實(shí)內(nèi)容
? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? self.text=[NSString stringWithFormat:@"倒計(jì)時(shí)時(shí)間? %@",format_time];
? ? ? ? ? ? });
? ? ? ? ? ?timeOut --;
? ? ? ? }
? ? });
? ?dispatch_resume(_timer); ??
}
創(chuàng)建UILable 進(jìn)行顯示,?
[self.timeLabel startSumTimes:@"2018-11-10 19:00:00"];
后續(xù)Dome 待上傳