zoukankan      html  css  js  c++  java
  • NSTimer 实现到一个指定时间(年、月、日)的倒计时

    //模仿抢购到记时

    //用指定格式dateFormatter构建date实例,获取构建的这个时间与当前时间[NSDate date]的时间差(秒数)

    - (NSString *)countDown
    {
      NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
      [dateFormatter setDateFormat:@”yyyy-MM-dd HH-mm-ss”];
      NSDate *date = [dateFormatter dateFormString:@”2015-12-18 9:15:00”];
      NSInteger poorDate = [date timeIntervalSinceDate:[NSDate date]];
      NSInteger minute = poorDate / 60;//计算分钟数
      if (!poorDate)
      {
        self.lable.text = @”活动正在进行,赶快抢购吧。。。”;
        self.button.enabled = YES;//当时间差为0时,是抢购按钮为激活状态
        [self.timer invalidate];//停止倒计时
      }
      if (minute >= 60)
      {
        NSInteger hour = minute / 60;//计算小时
        if (hour >= 24)
        {
          NSInteger day = hour / 24;//计算天
          return [NSString stringWithFormString:@”离开始还有%02ld天%02ld小时%02ld分钟%02ld秒”,day,hour % 24,minute % 60,poorDate % 60];
        }
        else
          return [[NSString stringWithFormString:@”离开始还有%02d天%02ld小时%02ld分钟%02ld秒”,0,hour,minute % 60,poorDate % 60];
      }
      else
        return [NSString stringWithFormString:@”离开始还有%02d天%02d小时%02ld分钟%02ld秒”,0,0,minute,poorDate % 60];
    }

    - (void)testRepeatTimer
    {
      if (!timer)//构建一个timer_实例,时间间隔为1秒,self调用倒计时方法(countDown),repeats需要重复
      {
        timer_ = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
      }
    }

  • 相关阅读:
    经典测试面试题一
    自定义函数实现字符串数组互转
    php 实现密码错误三次锁定账号10分钟
    HTTP状态码
    Linux知识点
    慢查日志
    curl 采集
    HTTP协议
    docker配置
    mysql数据库索引、存储引擎、事务
  • 原文地址:https://www.cnblogs.com/tang910103/p/5058500.html
Copyright © 2011-2022 走看看