// // KBCountDownView.m // YouHuiProject // // Created by 小花 on 2018/1/17. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBCountDownView.h" #define TimerFont [UIFont systemFontOfSize:13] @interface KBCountDownView (){ NSUInteger expiresTime; NSUInteger nowTime; NSDate *date1970; NSDateFormatter *dateFormatter; NSUInteger lastSeconds; NSInteger aDay; } @property (nonatomic, strong) dispatch_source_t timer; /**小时*/ @property (nonatomic,strong) UIButton *hourBtn; @property (nonatomic,strong) UIButton *hourBtn2; /**分钟*/ @property (nonatomic,strong) UIButton *minuteBtn; @property (nonatomic,strong) UIButton *minuteBtn2; /**秒*/ @property (nonatomic,strong) UIButton *secondBtn; @property (nonatomic,strong) UIButton *secondBtn2; /**:*/ @property (nonatomic,strong) UIButton *textBtn; @property (nonatomic,strong) UIButton *textBtn1; @property (nonatomic,strong) UIButton *textBtn2; @property (nonatomic,strong) UIButton *textBtn3; @end static KBCountDownView *_countDownView = nil; @implementation KBCountDownView + (instancetype)shareInstace { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _countDownView = [[KBCountDownView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)]; }); return _countDownView; } - (instancetype)initWithFrame:(CGRect)frame andEndTimer:(NSString *)endTimer { NSDate *date = [NSDate date]; NSTimeInterval timeinterval =[date timeIntervalSince1970]; NSTimeInterval endTime = timeinterval + endTimer.longLongValue; // NSString *timeString2 = [NSString stringWithFormat:@"%.0f", a2]; expiresTime = endTime; return [self initWithFrame:frame]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { date1970 = [NSDate dateWithTimeIntervalSince1970:0]; dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH mm ss"]; aDay = 86399; expiresTime = 0; nowTime = 0; [self addSubview:self.textBtn]; [self addSubview:self.hourBtn]; [self addSubview:self.hourBtn2]; [self addSubview:self.textBtn1]; [self addSubview:self.minuteBtn]; [self addSubview:self.minuteBtn2]; [self addSubview:self.textBtn2]; [self addSubview:self.secondBtn]; [self addSubview:self.secondBtn2]; [self addSubview:self.textBtn3]; } return self; } - (void)countDown { // 从1970年到现在的时间(秒) NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval second =[dat timeIntervalSince1970]; NSString *starTimer = [NSString stringWithFormat:@"%.0f", second]; nowTime = starTimer.longLongValue; __block NSUInteger timeout= expiresTime; //倒计时时间 NSString *curStr = [self homeLimitTimeString]; [self labelValueHandler:curStr]; if (_timer) { dispatch_source_cancel(_timer); } dispatch_queue_t queue = 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*NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_timer, ^{ if(timeout<=starTimer.longLongValue){ //倒计时结束,关闭 dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ }); if (self.delegate && [self.delegate respondsToSelector:@selector(countViewTimeOutAction)]) { [self.delegate countViewTimeOutAction]; } }else{ NSString *curStr1 = [self homeLimitTimeString]; dispatch_async(dispatch_get_main_queue(), ^{ [self labelValueHandler:curStr1]; }); timeout--; } }); dispatch_resume(_timer); } - (void)setEndTimer:(NSString *)endTimer { NSDate *date = [NSDate date]; NSTimeInterval timeinterval =[date timeIntervalSince1970]; NSString *endTimeStr = [NSString stringWithFormat:@"%.0f",timeinterval]; NSTimeInterval endTime = endTimeStr.longLongValue + endTimer.longLongValue/1000; expiresTime = endTime; //到期的时间戳 // NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0]; // NSTimeInterval second =[dat timeIntervalSince1970]; // NSString *starTimer = [NSString stringWithFormat:@"%.0f", second]; // nowTime = starTimer.longLongValue; [self countDown]; } -(NSString*)homeLimitTimeString{ NSTimeInterval timeActualValue; if (expiresTime