iOS中 简单易懂的秒杀倒计时/倒计时

简介:

示例代码简单易懂:

每日更新关注:http://weibo.com/hanjunqiang  新浪微博

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5. @property (weak, nonatomic) IBOutlet UILabel *dayLabel;  
  6. @property (weak, nonatomic) IBOutlet UILabel *hourLabel;  
  7. @property (weak, nonatomic) IBOutlet UILabel *minuteLabel;  
  8. @property (weak, nonatomic) IBOutlet UILabel *secondLabel;  
  9.   
  10. @end  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4. {  
  5.      dispatch_source_t _timer;  
  6. }  
  7. @end  
  8.   
  9. @implementation ViewController  
  10. /** 
  11.  *  获取当天的年月日的字符串 
  12.  *  这里测试用 
  13.  *  @return 格式为年-月-日 
  14.  */  
  15. -(NSString *)getyyyymmdd{  
  16.     NSDate *now = [NSDate date];  
  17.     NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];  
  18.     formatDay.dateFormat = @"yyyy-MM-dd";  
  19.     NSString *dayStr = [formatDay stringFromDate:now];  
  20.       
  21.     return dayStr;  
  22.       
  23. }  
  24. - (void)viewDidLoad {  
  25.     [super viewDidLoad];  
  26.     NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];  
  27.     [dateFormatter setDateFormat:@"yyyy-MM-dd"];  
  28.       
  29.     NSDate *endDate = [dateFormatter dateFromString:[self getyyyymmdd]];  
  30.     NSDate *endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate] + 24*3600)];  
  31.     NSDate *startDate = [NSDate date];  
  32.     NSTimeInterval timeInterval =[endDate_tomorrow timeIntervalSinceDate:startDate];  
  33.       
  34.     if (_timer==nil) {  
  35.         __block int timeout = timeInterval; //倒计时时间  
  36.           
  37.         if (timeout!=0) {  
  38.             dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
  39.             _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 00,queue);  
  40.             dispatch_source_set_timer(_timer,dispatch_walltime(NULL0),1.0*NSEC_PER_SEC, 0); //每秒执行  
  41.             dispatch_source_set_event_handler(_timer, ^{  
  42.                 if(timeout<=0){ //倒计时结束,关闭  
  43.                     dispatch_source_cancel(_timer);  
  44.                     _timer = nil;  
  45.                     dispatch_async(dispatch_get_main_queue(), ^{  
  46.                         self.dayLabel.text = @"";  
  47.                         self.hourLabel.text = @"00";  
  48.                         self.minuteLabel.text = @"00";  
  49.                         self.secondLabel.text = @"00";  
  50.                     });  
  51.                 }else{  
  52.                     int days = (int)(timeout/(3600*24));  
  53.                     if (days==0) {  
  54.                         self.dayLabel.text = @"";  
  55.                     }  
  56.                     int hours = (int)((timeout-days*24*3600)/3600);  
  57.                     int minute = (int)(timeout-days*24*3600-hours*3600)/60;  
  58.                     int second = timeout-days*24*3600-hours*3600-minute*60;  
  59.                     dispatch_async(dispatch_get_main_queue(), ^{  
  60.                         if (days==0) {  
  61.                             self.dayLabel.text = @"0天";  
  62.                         }else{  
  63.                             self.dayLabel.text = [NSString stringWithFormat:@"%d天",days];  
  64.                         }  
  65.                         if (hours<10) {  
  66.                             self.hourLabel.text = [NSString stringWithFormat:@"0%d",hours];  
  67.                         }else{  
  68.                             self.hourLabel.text = [NSString stringWithFormat:@"%d",hours];  
  69.                         }  
  70.                         if (minute<10) {  
  71.                             self.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];  
  72.                         }else{  
  73.                             self.minuteLabel.text = [NSString stringWithFormat:@"%d",minute];  
  74.                         }  
  75.                         if (second<10) {  
  76.                             self.secondLabel.text = [NSString stringWithFormat:@"0%d",second];  
  77.                         }else{  
  78.                             self.secondLabel.text = [NSString stringWithFormat:@"%d",second];  
  79.                         }  
  80.                           
  81.                     });  
  82.                     timeout--;  
  83.                 }  
  84.             });  
  85.             dispatch_resume(_timer);  
  86.         }  
  87.     }  
  88.   
  89.   
  90. }  
每日更新关注:http://weibo.com/hanjunqiang  新浪微博

效果:


每日更新关注:http://weibo.com/hanjunqiang  新浪微博

Demo下载地址GitHub:https://github.com/XiaoHanGe/CountDown

原文地址: http://blog.csdn.net/qq_31810357/article/details/50700292
相关文章
|
API iOS开发 Perl
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
|
iOS开发
iOS开发-三种倒计时的写法
iOS开发-三种倒计时的写法
183 0
|
程序员 定位技术 iOS开发
iOS开发:解决App进入后台,倒计时(定时器)不能正常计时的问题
在iOS开发过程中,尤其是发送短信验证码的需求是非常常见的需求,这就涉及到倒计时的使用,但是如果正在倒计时操作,app进入后台运行,倒计时会出现什么效果呢?那么本篇博文就来了解一下相关知识吧。
1237 1
iOS开发:解决App进入后台,倒计时(定时器)不能正常计时的问题
|
JavaScript iOS开发
js 实现倒计时功能,兼容ios,类似京东
js 实现倒计时功能,兼容ios,类似京东
166 0
js 实现倒计时功能,兼容ios,类似京东
|
iOS开发
iOS倒计时按钮闪烁问题解决
iOS倒计时按钮闪烁问题解决
133 0
|
iOS开发
iOS 特价秒杀(轮播图内嵌倒计时)
iOS 特价秒杀(轮播图内嵌倒计时)
130 0
|
27天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0