多个cell上的倒计时,超时后变成正计时

先上图:


Simulator Screen Shot 2016年11月30日 上午10.53.24.png
用到了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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容