123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- //
- // DRCountDownView.m
- // YouHuiProject
- //
- // Created by 小花 on 2018/1/17.
- // Copyright © 2018年 kuxuan. All rights reserved.
- //
- #import "DRCountDownView.h"
- #define TimerFont [UIFont systemFontOfSize:13]
- @interface DRCountDownView (){
- 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 DRCountDownView *_countDownView = nil;
- @implementation DRCountDownView
- + (instancetype)shareInstace {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- _countDownView = [[DRCountDownView 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<nowTime) {
- return @"00 00 00";
- }
- NSTimeInterval diffTime = expiresTime - nowTime;
- // NSLog(@"%ld",(expiresTime - nowTime)/3600/24); /// 这里可以设置天数
- timeActualValue = diffTime;
- nowTime++;
-
- NSDate *curDate = [date1970 dateByAddingTimeInterval:timeActualValue];
- NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-0800"];
- NSInteger interval = [zone secondsFromGMTForDate: curDate];
- NSDate *localeDate = [curDate dateByAddingTimeInterval: interval];
- NSString *destDateString = [dateFormatter stringFromDate:localeDate];
-
- return destDateString;
- }
- -(void)labelValueHandler:(NSString*)text{
- NSArray *titleArr = [text componentsSeparatedByString:@" "];
- NSString *hourStr = titleArr[0];
- NSString *minStr = titleArr[1];
- NSString *secondStr = titleArr[2];
-
- [self.hourBtn setTitle:[hourStr substringWithRange:NSMakeRange(0, 1)] forState:UIControlStateNormal];
- [self.hourBtn2 setTitle:[hourStr substringWithRange:NSMakeRange(1, 1)] forState:UIControlStateNormal];
-
- [self.minuteBtn setTitle:[minStr substringWithRange:NSMakeRange(0, 1)] forState:UIControlStateNormal];
- [self.minuteBtn2 setTitle:[minStr substringWithRange:NSMakeRange(1, 1)] forState:UIControlStateNormal];
-
- [self.secondBtn setTitle:[secondStr substringWithRange:NSMakeRange(0, 1)] forState:UIControlStateNormal];
- [self.secondBtn2 setTitle:[secondStr substringWithRange:NSMakeRange(1, 1)] forState:UIControlStateNormal];
- }
- #pragma mark =============== layzer ==================
- - (UIButton *)textBtn
- {
- if (_textBtn == nil) {
- _textBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _textBtn.frame = CGRectMake(0, 0, 50, self.frame.size.height);
- _textBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
- [_textBtn setTitle:@"距更新" forState:UIControlStateNormal];
- _textBtn.titleLabel.font = [UIFont systemFontOfSize:12];
- [_textBtn setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
- }
- return _textBtn;
- }
- - (UIButton *)hourBtn
- {
- if (_hourBtn == nil) {
- _hourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _hourBtn.frame = CGRectMake(CGRectGetMaxX(_textBtn.frame), 0, 12, self.frame.size.height);
- _hourBtn.backgroundColor = [UIColor YHColorWithHex:0x444444];
- _hourBtn.titleLabel.font = TimerFont;
- _hourBtn.layer.cornerRadius = 3;
- }
- return _hourBtn;
- }
- - (UIButton *)hourBtn2
- {
- if (_hourBtn2 == nil) {
- _hourBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
- _hourBtn2.frame = CGRectMake(CGRectGetMaxX(_hourBtn.frame)+2, 0, 12, self.frame.size.height);
- _hourBtn2.backgroundColor = [UIColor YHColorWithHex:0x444444];
- _hourBtn2.titleLabel.font = TimerFont;
- _hourBtn2.layer.cornerRadius = 3;
- }
- return _hourBtn2;
- }
- - (UIButton *)textBtn1
- {
- if (_textBtn1 == nil) {
- _textBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
- _textBtn1.frame = CGRectMake(CGRectGetMaxX(_hourBtn2.frame), 0, 20, _hourBtn.frame.size.height);
- _textBtn1.titleLabel.textAlignment = NSTextAlignmentCenter;
- [_textBtn1 setTitle:@"时" forState:UIControlStateNormal];
- _textBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
- [_textBtn1 setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
- }
- return _textBtn1;
- }
- - (UIButton *)minuteBtn
- {
- if (_minuteBtn == nil) {
- _minuteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _minuteBtn.frame = CGRectMake(CGRectGetMaxX(_textBtn1.frame),0 , 12, _hourBtn.frame.size.height);
- _minuteBtn.titleLabel.font = TimerFont;
- _minuteBtn.backgroundColor = [UIColor YHColorWithHex:0x444444];
- _minuteBtn.layer.cornerRadius = 3;
- }
- return _minuteBtn;
- }
- - (UIButton *)minuteBtn2
- {
- if (_minuteBtn2 == nil) {
- _minuteBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
- _minuteBtn2.frame = CGRectMake(CGRectGetMaxX(_minuteBtn.frame)+2,0 , 12, _hourBtn.frame.size.height);
- _minuteBtn2.titleLabel.font = TimerFont;
- _minuteBtn2.backgroundColor = [UIColor YHColorWithHex:0x444444];
- _minuteBtn2.layer.cornerRadius = 3;
- }
- return _minuteBtn2;
- }
- - (UIButton *)textBtn2
- {
- if (_textBtn2 == nil) {
- _textBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
- _textBtn2.frame = CGRectMake(CGRectGetMaxX(_minuteBtn2.frame), 0, 20, _hourBtn.frame.size.height);
- _textBtn2.titleLabel.textAlignment = NSTextAlignmentCenter;
- [_textBtn2 setTitle:@"分" forState:UIControlStateNormal];
- _textBtn2.titleLabel.font = [UIFont systemFontOfSize:12];
- [_textBtn2 setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
- }
- return _textBtn2;
- }
- - (UIButton *)secondBtn
- {
- if (_secondBtn == nil) {
- _secondBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _secondBtn.frame = CGRectMake(CGRectGetMaxX(_textBtn2.frame), 0, 12,_hourBtn.frame.size.height);
- _secondBtn.backgroundColor = [UIColor YHColorWithHex:0x444444];
- _secondBtn.titleLabel.font = TimerFont;
- _secondBtn.layer.cornerRadius = 3;
-
- }
- return _secondBtn;
- }
- - (UIButton *)secondBtn2
- {
- if (_secondBtn2 == nil) {
- _secondBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
- _secondBtn2.frame = CGRectMake(CGRectGetMaxX(_secondBtn.frame)+2, 0, 12,_hourBtn.frame.size.height);
- _secondBtn2.backgroundColor = [UIColor YHColorWithHex:0x444444];
- _secondBtn2.titleLabel.font = TimerFont;
- _secondBtn2.layer.cornerRadius = 3;
-
- }
- return _secondBtn2;
- }
- - (UIButton *)textBtn3
- {
- if (_textBtn3 == nil) {
- _textBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
- _textBtn3.frame = CGRectMake(CGRectGetMaxX(_secondBtn2.frame), 0, 20, _hourBtn.frame.size.height);
- _textBtn3.titleLabel.textAlignment = NSTextAlignmentCenter;
- [_textBtn3 setTitle:@"秒" forState:UIControlStateNormal];
- _textBtn3.titleLabel.font = [UIFont systemFontOfSize:12];
- [_textBtn3 setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
- }
- return _textBtn3;
- }
- @end
|