先上图:
用到了NSDateComponents和NSCalendar这俩类
NSDateComponents:可以获取到给定的起始日期到结束日期还剩余的 年/月/日/时/分/秒/毫秒,超出结束日期 ,获取出的值为负的. 思路就是创建一个定时器 每秒获取一下NSDateComponents对象,取出时间.然后通过block在cell上刷新时间
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
_flags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components =
[calendar components:_flags fromDate:[NSDate date] toDate:_endTime options:NSCalendarWrapComponents];
_hour = labs(components.hour);
_minute = labs(components.minute);
_second = labs(components.second);
_isEnd = components.second < 0 ? YES : NO;
if (self.timechanged) {
self.timechanged();
}
不费话了,上Demo