网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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];

    }

}


网页标题:iOS时间分钟秒的倒计时
本文路径:http://bjjierui.cn/article/jdgjji.html

其他资讯