-(void)timeDeal{
// 倒计时的时间 测试数据
NSString *deadlineStr = self.model.follow_time;
NSLog(@"deadlinStr=%@",deadlineStr);
// 当前时间的时间戳
NSString *nowStr = [self getCurrentTimeyyyymmdd];
NSLog(@"nowStr=%@",nowStr);
// 计算时间差值
NSInteger secondsCountDown = [self getDateDifferenceWithNowDateStr:deadlineStr deadlineStr:nowStr];
NSLog(@"secondsCountDown=%ld",secondsCountDown);
__weak __typeof(self) weakSelf = self;
if (_timer == nil) {
__block NSInteger timeout = secondsCountDown; // 倒计时时间
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); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout <= 0){ // 当倒计时结束时做需要的操作: 关闭 活动到期不能提交
dispatch_source_cancel(_timer);
_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.FollowUpLabel.text = @"时间已到";
});
} else { // 倒计时重新计算 时/分/秒
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:@"预警跟进 %02ld : %02ld : %02ld", hours, minute, second];
dispatch_async(dispatch_get_main_queue(), ^{
if (days == 0) {
weakSelf.FollowUpLabel.text = @"正常跟进";
} else {
NSString *followString = [NSString stringWithFormat:@"预警跟进 %ld天 %02ld : %02ld : %02ld", days, hours, minute, second];
NSMutableAttributedString *attrring = [[NSMutableAttributedString alloc] initWithString:followString];
[attrring addAttribute:NSForegroundColorAttributeName value:TEXT_COLOR_RED range:NSMakeRange(0, 4)];
self.FollowUpLabel.attributedText = attrring;
}
});
timeout--; // 递减 倒计时-1(总时间以秒来计算)
}
});
dispatch_resume(_timer);
}
}
}
//获取当前时间
- (NSString *)getCurrentTimeyyyymmdd {
NSDate *now = [NSDate date];
NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
formatDay.dateFormat = @"yyyy-MM-dd HH:mm";
NSString *dayStr = [formatDay stringFromDate:now];
return dayStr;
}
//获取时间戳
- (NSInteger)getDateDifferenceWithNowDateStr:(NSString*)nowDateStr deadlineStr:(NSString*)deadlineStr {
NSInteger timeDifference = 0;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yy-MM-dd HH:mm"];
NSDate *nowDate = [formatter dateFromString:nowDateStr];
NSDate *deadline = [formatter dateFromString:deadlineStr];
NSTimeInterval oldTime = [nowDate timeIntervalSince1970];
NSTimeInterval newTime = [deadline timeIntervalSince1970];
timeDifference = newTime - oldTime;
return timeDifference;
}
网友评论