iOS倒计时,显示剩余天、时、分、秒
2016-01-07 18:09
435 查看
iOS倒计时的实现,显示剩余天、时、分、秒
做项目中经常会遇到秒杀、抢商品啊等等。那么这个十分秒的倒数是如何代码实现的呢!Demo地址:https://github.com/zhengwenming/countDown
通常后台会给我们一个时间戳活着截至日期(deadLine)。那么无论是时间戳还是一个具体的日期时间点,我们的处理逻辑都是这样的。统一处理成NSDate对象,那么就起名叫做endDate吧。我们还有一个开始时间,就是当前时间,命名为startDate。
我们要取到endDate和startDate的间隔有多久,有多长的时间间隔。那么废话少说,开始看代码,这样更清晰。
//结束时间
detailedDict[@”endTime”]为服务端返回的数据。
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
if ([detailedDict[@”endTime”] length]==10) {
[dateFormatter setDateFormat:@”yyyy-MM-dd”];
}else{
[dateFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];
}
// NSDate *endDate = [dateFormatter dateFromString:detailedDict[@”endTime”]];
NSDate *endDate = [dateFormatter dateFromString:@”2016-02-20”];
那么这里注意了,如果后台给你的是这样的具体日期,那么我们还要多加一天的时间,毕竟要倒计时到 2016-02-20日的深夜0:00啊,下一秒就是2016-02-21日了。如果后台给的是时间戳,那么不用多加一天,因为时间戳就是个具体的时间点。
NSDate *endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate] + 24*3600)];
//当前时间
NSDate *startDate = [NSDate date];
//得到相差秒数
NSTimeInterval timeInterval =[endDate_tomorrow timeIntervalSinceDate:startDate];
下面处理UI显示的逻辑
if (timeInterval==0) {
detailCell.yzImageView.hidden = NO;
过期了,倒计时结束了
}else{
detailCell.yzImageView.hidden = YES;
没过期, 倒计时还会继续
}
这里用到这个 dispatch_source_t _timer;
把timer定义为全局的。
if (_timer==nil) { __block int timeout = timeInterval; //倒计时时间 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(), ^{ detailCell.dayLabel.text = @""; detailCell.hourLabel.text = @"00"; detailCell.minuteLabel.text = @"00"; detailCell.secondLabel.text = @"00"; detailCell.yzImageView.hidden = NO; }); }else{ int days = (int)(timeout/(3600*24)); if (days==0) { detailCell.dayLabel.text = @""; } int hours = (int)((timeout-days*24*3600)/3600); int minute = (int)(timeout-days*24*3600-hours*3600)/60; int second = timeout-days*24*3600-hours*3600-minute*60; dispatch_async(dispatch_get_main_queue(), ^{ if (days==0) { detailCell.dayLabel.text = @""; }else{ detailCell.dayLabel.text = [NSString stringWithFormat:@"%d天",days]; } if (hours<10) { detailCell.hourLabel.text = [NSString stringWithFormat:@"0%d",hours]; }else{ detailCell.hourLabel.text = [NSString stringWithFormat:@"%d",hours]; } if (minute<10) { detailCell.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute]; }else{ detailCell.minuteLabel.text = [NSString stringWithFormat:@"%d",minute]; } if (second<10) { detailCell.secondLabel.text = [NSString stringWithFormat:@"0%d",second]; }else{ detailCell.secondLabel.text = [NSString stringWithFormat:@"%d",second]; } }); timeout--; } }); dispatch_resume(_timer); } }
上面的时间分别显示为 00天 3:35:49
天 时:分:秒
用四个UILabel分开来显示。因为我们可以算出时间间隔所对应的天、时、分、秒。然后每秒赋值一次就可以了。
每次进来这个页面都是从服务器获取最新的endDate进行倒计时。这样保证前端和后台的数据一致。
相关文章推荐
- iOS 解决LaunchScreen中图片加载黑屏问题
- RedHat 6.5(x86_64)启动nagios客户端nrpe报错的解决方法
- iOS常见手势总结-单击、双击、捏合、旋转、移动、清扫、长按
- iOS 支付宝的使用
- IOS项目分层
- iOS开发零碎笔记
- iOS开发常用的第三方类库
- iOS开发官方文档
- iOS常见错误之exc_bad_access 野指针错误
- iOS开发多线程
- iOS成长之AutoLayout写控件的注意事项
- ios测试基础七:常用软件
- iOS多线程中的dispatch_semaphore_t
- iOS 开发笔记
- IOS-添加分段控件SegmentControl
- 2016最新iOS开发者证书申请及安装教程
- IOS 让tableview选中效果渐变消失
- iOS崩溃调试
- iOS下int long longlong的取值范围
- IOS Replaykit 手游录屏Demo