IOS development code sharing with Nstimer to achieve the countdown function _ios

Source: Internet
Author: User
Tags current time

With Nstimer implementation countdown function, nonsense not much said, directly on the code, detailed explanation please refer to comments

[Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (timerfiremethod:) Userinfo:nil Repeats 
: YES]; 
 /-(void) Timerfiremethod: (Nstimer *) Thetimer {    bool Timestart = YES;     nscalendar *cal = [Nscalendar currentcalendar];//defines a Nscalendar object      Nsdatecomponents *endtime = [[Nsdatecomponents alloc] init];   //initialization target time ...      NSDate *today = [nsdate date];   //Get current time            
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) {//from the NSDate to take out the month, time seconds, but only once         
 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 (one, 2)] intValue];         minute = [[Overdate substringwithrange:nsmakerange (2)] IntValue 
];         second = [[Overdate substringwithrange:nsmakerange (2)] IntValue 
];    &nbsP;    timestart= NO; 
   &NBSP}           [endtime setyear:year]; 
    [endtime Setmonth:month]; 
    [endtime Setday:day]; 
    [endtime Sethour:hour]; 
    [endtime Setminute:minute]; 
    [endtime Setsecond:second];     nsdate *overtime = [cal Datefromcomponents:endtime]; The target time is loaded into date     //is used to get the specific time difference, which is to unify into the Beijing times     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] <) {        fen = [NSString 
stringwithformat:@ "0%d", [D minute]]; 
&NBSP;&NBSP;&NBSP;&NBSP}     nsstring *miao = [NSString stringwithformat:@ "%d", [d second]];     if ([D Second] <) {        miao = [NSString 
stringwithformat:@ "0%d", [d second]]; 
&NBSP;&NBSP;&NBSP;&NBSP}//    NSLog (@ "===%@ day%@:%@:%@", T,h,fen,miao); 
    [_longtime settext:[nsstring stringwithformat:@ "%@ Day%@:%@:%@", T,h,fen,miao];     if ([D second] > 0) {        //Timing is not yet over, Do_ Something//        [_longtime settext:[nsstring stringWithFormat:@ "%@:%@:%@", D 
, Fen,miao]]; &NBSP;&NBSP;&NBSP;&NBSP} else if ([d second] = = 0) {        //Timing End Do_something            &NBSP;&NBSP;&NBSP;&NBSP} else{//timer failure         [thetimer invalidate 
]; &NBSP;&NBSP;&NBSP;&NBSP}      }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.