欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS开发代码分享之用nstimer实现倒计时功能

程序员文章站 2023-12-28 20:58:16
用nstimer实现倒计时功能,废话不多说,直接上代码,详细解释请参照注释 // [nstimer scheduledtimerwithtimeinterva...

用nstimer实现倒计时功能,废话不多说,直接上代码,详细解释请参照注释

// 
[nstimer scheduledtimerwithtimeinterval:1.0 target:self selector:@selector(timerfiremethod:) userinfo:nil repeats:yes]; 
 
// 
- (void)timerfiremethod:(nstimer *)thetimer 
{ 
    bool timestart = yes; 
    nscalendar *cal = [nscalendar currentcalendar];//定义一个nscalendar对象 
    nsdatecomponents *endtime = [[nsdatecomponents alloc] init];    //初始化目标时间... 
    nsdate *today = [nsdate date];    //得到当前时间 
     
    nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; 
    [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; 
    nsdate *datestring = [dateformatter datefromstring:todate]; 
    nsstring *overdate = [dateformatter stringfromdate:datestring]; 
//    nslog(@"overdate=%@",overdate); 
    static int year; 
    static int month; 
    static int day; 
    static int hour; 
    static int minute; 
    static int second; 
    if(timestart) {//从nsdate中取出年月日,时分秒,但是只能取一次 
        year = [[overdate substringwithrange:nsmakerange(0, 4)] intvalue]; 
        month = [[overdate substringwithrange:nsmakerange(5, 2)] intvalue]; 
        day = [[overdate substringwithrange:nsmakerange(8, 2)] intvalue]; 
        hour = [[overdate substringwithrange:nsmakerange(11, 2)] intvalue]; 
        minute = [[overdate substringwithrange:nsmakerange(14, 2)] intvalue]; 
        second = [[overdate 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 *overtime = [cal datefromcomponents:endtime]; //把目标时间装载入date 
    //用来得到具体的时差,是为了统一成北京时间 
    unsigned int unitflags = nsyearcalendarunit| nsmonthcalendarunit| nsdaycalendarunit| nshourcalendarunit| nsminutecalendarunit| nssecondcalendarunit; 
    nsdatecomponents *d = [cal components:unitflags fromdate:today todate:overtime options:0]; 
    nsstring *t = [nsstring stringwithformat:@"%d", [d day]]; 
    nsstring *h = [nsstring stringwithformat:@"%d", [d hour]]; 
    nsstring *fen = [nsstring stringwithformat:@"%d", [d minute]]; 
    if([d minute] < 10) { 
        fen = [nsstring stringwithformat:@"0%d",[d minute]]; 
    } 
    nsstring *miao = [nsstring stringwithformat:@"%d", [d second]]; 
    if([d second] < 10) { 
        miao = [nsstring stringwithformat:@"0%d",[d second]]; 
    } 
//    nslog(@"===%@天 %@:%@:%@",t,h,fen,miao); 
    [_longtime settext:[nsstring stringwithformat:@"%@天 %@:%@:%@",t,h,fen,miao]]; 
    if([d second] > 0) { 
        //计时尚未结束,do_something 
//        [_longtime settext:[nsstring stringwithformat:@"%@:%@:%@",d,fen,miao]]; 
    } else if([d second] == 0) { 
        //计时结束 do_something 
         
    } else{ 
//计时器失效
        [thetimer invalidate]; 
    } 
     
}

上一篇:

下一篇: