iOS时间分钟秒的倒计时-创新互联

#import "ViewController.h"

为原州等地区用户提供了全套网页设计制作服务,及原州网站建设行业解决方案。主营业务为网站建设、网站制作、原州网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

@interface ViewController ()

{

  BOOL timeStart;

  NSMutableArray *savaTimeArry;

}

@end

@implementation ViewController

- (void)viewDidLoad {

  [super viewDidLoad];

  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

  timeStart = YES;

}

- (void)timerFireMethod:(NSTimer *)theTimer

{

  NSCalendar *cal = [NSCalendar currentCalendar];//定义一个NSCalendar对象

  NSDateComponents *endTime = [[NSDateComponents alloc] init];  //初始化目标时间...

  NSDate *today = [NSDate date];  //得到当前时间

  NSDate *date = [NSDate dateWithTimeInterval:117000 sinceDate:today];

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

  [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

  NSString *dateString = [dateFormatter stringFromDate:date];

  static int year;

  static int month;

  static int day;

  static int hour;

  static int minute;

  static int second;

  if(timeStart)

  {//从NSDate中取出年月日,时分秒,但是只能取一次

    year = [[dateString substringWithRange:NSMakeRange(0, 4)] intValue];

    month = [[dateString substringWithRange:NSMakeRange(5, 2)] intValue];

    day = [[dateString substringWithRange:NSMakeRange(8, 2)] intValue];

    hour = [[dateString substringWithRange:NSMakeRange(11, 2)] intValue];

    minute = [[dateString substringWithRange:NSMakeRange(14, 2)] intValue];

    second = [[dateString substringWithRange:NSMakeRange(17, 2)] intValue];

    timeStart= NO;

  }

  [endTime setYear:year];

  [endTime setMonth:month];

  [endTime setDay:day];

  [endTime setHour:hour];

  [endTime setMinute:minute];

  [endTime setSecond:second];

  NSDate *todate = [cal dateFromComponents:endTime]; //把目标时间装载入date

  //用来得到具体的时差,是为了统一成北京时间

  unsigned int unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit| NSDayCalendarUnit| NSHourCalendarUnit| NSMinuteCalendarUnit| NSSecondCalendarUnit;

  NSDateComponents *d = [cal components:unitFlags fromDate:today toDate:todate options:0];

  NSString *fen = [NSString stringWithFormat:@"%ld", [d minute]];

  NSString *hou = [NSString stringWithFormat:@"%ld",[d hour]];

  if([d hour]<10)

  {

    hou = [NSString stringWithFormat:@"0%ld",[d hour]];

  }

  if([d minute] < 10) {

    fen = [NSString stringWithFormat:@"0%ld",[d minute]];

  }

  NSString *miao = [NSString stringWithFormat:@"%ld", [d second]];

  if([d second] < 10) {

    miao = [NSString stringWithFormat:@"0%ld",[d second]];

  }

  if([d second] > 0)

  {

    savaTimeArry = [NSMutableArray array];

    NSString *shouTimer= [NSString stringWithFormat:@"%@%@%@",hou,fen,miao];

    NSString *b1= [shouTimer substringWithRange:NSMakeRange(0,1)];

    NSString *b2= [shouTimer substringWithRange:NSMakeRange(1,1)];

    NSString *b3= [shouTimer substringWithRange:NSMakeRange(2,1)];

    NSString *b4= [shouTimer substringWithRange:NSMakeRange(3,1)];

    NSString *b5= [shouTimer substringWithRange:NSMakeRange(4,1)];

    NSString *b6= [shouTimer substringWithRange:NSMakeRange(5,1)];

    [savaTimeArry addObject:b1];

    [savaTimeArry addObject:b2];

    [savaTimeArry addObject:b3];

    [savaTimeArry addObject:b4];

    [savaTimeArry addObject:b5];

    [savaTimeArry addObject:b6];

    [self showtimeView];

  } else if([d second] == 0)

  {

    //计时1分钟结束,do_something

  } else

  {

    [theTimer invalidate];

  }

}

- (void)showtimeView

{

  for(int i = 0 ;i <6;i++)

  {

    UIButton *butom = [UIButton buttonWithType:UIButtonTypeCustom];

    butom.frame = CGRectMake(10+10+i*10+2*i+(int)(i/2)*5, 100, 10, 20);

    butom.backgroundColor = [UIColor blackColor];

    [butom setTitle:[savaTimeArry objectAtIndex:i] forState:UIControlStateNormal];

    [butom setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    butom.titleLabel.font = [UIFont systemFontOfSize:14.0f];

    [self.view addSubview:butom];

  }

}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页题目:iOS时间分钟秒的倒计时-创新互联
链接分享:http://ybzwz.com/article/pigdh.html