iOS实现秒杀活动倒计时

前端之家收集整理的这篇文章主要介绍了iOS实现秒杀活动倒计时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

IOS关于大型网站抢购、距活动结束,剩余时间倒计时的实现代码代码比较简单,大家根据需求适当的添加修改删除代码

iOS实现秒杀活动倒计时


1.定义4个 Label 来接收倒计时:

  1. @property (weak,nonatomic) IBOutlet UILabel *dayLabel;
  2. @property (weak,nonatomic) IBOutlet UILabel *hourLabel;
  3. @property (weak,nonatomic) IBOutlet UILabel *minuteLabel;
  4. @property (weak,nonatomic) IBOutlet UILabel *secondLabel;

2.在实现文件实现方法:

  1. //时间戳转换为日期格式(毫秒的时间戳)
  2. - (NSString *)timeWithTimeIntervalString:(NSString *)timeString
  3. {
  4. // 格式化时间
  5. NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
  6. formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
  7. [formatter setDateStyle:NSDateFormatterMediumStyle];
  8. [formatter setTimeStyle:NSDateFormatterShortStyle];
  9. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  10.  
  11. // 毫秒值转化为秒
  12. NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
  13. NSString* dateString = [formatter stringFromDate:date];
  14. NSLog(@"时间 === %@",dateString);
  15. return dateString;
  16. }
  17. -(void)downSecondHandle:(NSString *)aTimeString{
  18.  
  19. NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
  20. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  21.  
  22. NSDate *endDate = [dateFormatter dateFromString:[self timeWithTimeIntervalString:aTimeString]]; //结束时间
  23. NSDate *endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate])];
  24. NSDate *startDate = [NSDate date];
  25. NSString* dateString = [dateFormatter stringFromDate:startDate];
  26. NSLog(@"现在的时间 === %@",dateString);
  27. NSTimeInterval timeInterval =[endDate_tomorrow timeIntervalSinceDate:startDate];
  28.  
  29. if (_timer==nil) {
  30. __block int timeout = timeInterval; //倒计时时间
  31.  
  32. if (timeout!=0) {
  33. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
  34. _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,queue);
  35. dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0); //每秒执行
  36. dispatch_source_set_event_handler(_timer,^{
  37. if(timeout<=0){ //倒计时结束,关闭
  38. dispatch_source_cancel(_timer);
  39. _timer = nil;
  40. dispatch_async(dispatch_get_main_queue(),^{
  41. self.dayLabel.text = @"";
  42. self.hourLabel.text = @"00";
  43. self.minuteLabel.text = @"00";
  44. self.secondLabel.text = @"00";
  45. });
  46. }else{
  47. int days = (int)(timeout/(3600*24));
  48. if (days==0) {
  49. self.dayLabel.text = @"";
  50. }
  51. int hours = (int)((timeout-days*24*3600)/3600);
  52. int minute = (int)(timeout-days*24*3600-hours*3600)/60;
  53. int second = timeout-days*24*3600-hours*3600-minute*60;
  54. dispatch_async(dispatch_get_main_queue(),^{
  55. if (days==0) {
  56. self.dayLabel.text = @"0天";
  57. }else{
  58. self.dayLabel.text = [NSString stringWithFormat:@"%d天",days];
  59. }
  60. if (hours<10) {
  61. self.hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
  62. }else{
  63. self.hourLabel.text = [NSString stringWithFormat:@"%d",hours];
  64. }
  65. if (minute<10) {
  66. self.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
  67. }else{
  68. self.minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
  69. }
  70. if (second<10) {
  71. self.secondLabel.text = [NSString stringWithFormat:@"0%d",second];
  72. }else{
  73. self.secondLabel.text = [NSString stringWithFormat:@"%d",second];
  74. }
  75.  
  76. });
  77. timeout--;
  78. }
  79. });
  80. dispatch_resume(_timer);
  81. }
  82. }
  83.  
  84. }

3.在需要出使用:

  1. [self downSecondHandle:@"1494622800000"];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的iOS相关文章