F3C993B5-1415-4F0F-B0E3-451AC2589D81.png
1 獲取當(dāng)前時(shí)間 和目標(biāo)時(shí)間 計(jì)算相差多少
2 使用gcd 獲取倒計(jì)時(shí)
3 屬性字符串 設(shè)置 時(shí)分秒 的字體和背景色
{
dispatch_source_t _timer;
}
NSString *deadlineStr = self.model.auction.endTime;
NSString *nowStr = [NSString getCurrentTimeyyyymmdd];
NSInteger secondsCountDown = [NSString getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];
[self setdata:secondsCountDown];
- (void)setdata:(NSInteger)secondsCountDown{
__weak __typeof(self) weakSelf = self;
if (_timer == nil) {
__block NSInteger timeout = secondsCountDown; // 倒計(jì)時(shí)時(shí)間
if (timeout!=0) {
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.0*NSEC_PER_SEC, 0); //每秒執(zhí)行
dispatch_source_set_event_handler(_timer, ^{
if(timeout <= 0){ // 當(dāng)?shù)褂?jì)時(shí)結(jié)束時(shí)做需要的操作: 關(guān)閉 活動(dòng)到期不能提交
dispatch_source_cancel(self->_timer);
self->_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
// weakSelf.timeLabel.text = @"當(dāng)前活動(dòng)已結(jié)束";
weakSelf.headerView.titleLabel.text = @"已結(jié)束";
});
} else { // 倒計(jì)時(shí)重新計(jì)算 時(shí)/分/秒
NSInteger days = (int)(timeout/(3600*24));
NSInteger hours = (int)((timeout-days*24*3600)/3600);
NSInteger minute = (int)(timeout-days*24*3600-hours*3600)/60;
NSInteger second = timeout - days*24*3600 - hours*3600 - minute*60;
NSString *strTime = [NSString stringWithFormat:@"距結(jié)束還有: %02ld 時(shí) %02ld 分 %02ld 秒",hours, minute,second];
dispatch_async(dispatch_get_main_queue(), ^{
if (days == 0) {
// weakSelf.headerView.titleLabel.text = strTime;
NSString *endstr = strTime;
NSMutableAttributedString *mutablestr = [[NSMutableAttributedString alloc]initWithString:endstr];
NSDictionary *attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName: ZHColor(140, 190, 250, 1),NSForegroundColorAttributeName:[UIColor whiteColor]};
[mutablestr setAttributes:attris range:NSMakeRange(7,2)];
[mutablestr setAttributes:attris range:NSMakeRange(12,2)];
[mutablestr setAttributes:attris range:NSMakeRange(17,2)];
weakSelf.headerView.titleLabel.attributedText = mutablestr;
} else {
NSString *endstr = [NSString stringWithFormat:@"距結(jié)束還有: %ld 天 %02ld 時(shí) %02ld 分 ",days,hours, minute];
NSMutableAttributedString *mutablestr = [[NSMutableAttributedString alloc]initWithString:endstr];
NSDictionary *attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName: ZHColor(140, 190, 250, 1),NSForegroundColorAttributeName:[UIColor whiteColor]};
[mutablestr setAttributes:attris range:NSMakeRange(7,2)];
[mutablestr setAttributes:attris range:NSMakeRange(12,2)];
[mutablestr setAttributes:attris range:NSMakeRange(17,2)];
weakSelf.headerView.titleLabel.attributedText = mutablestr;
}
});
timeout--; // 遞減 倒計(jì)時(shí)-1(總時(shí)間以秒來計(jì)算)
}
});
dispatch_resume(_timer);
}
}
}