How to countdown timer on hours and mins and stop when it's 0 , objective c

妖精的绣舞 提交于 2019-12-12 04:15:54

问题


  viewdidload

 {   
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] init];
    [df setDateFormat:@"HH:mm:ss"];

    ctime=([df stringFromDate:[NSDate date]]);
    NSDate *date1 = [df dateFromString:@"12:44"];`
    NSDate *date2 = [df dateFromString:@"20:50"];
    NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
    int hours = (int)interval / 3600;             // integer division to get the hours part
    int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
    NSString *timeDiff = [NSString stringWithFormat:@"%d:%02d", hours, minutes];
    currHour=hours;
    currMinute=minutes;
    currSeconds=00;
    [self start];
}

-(void)start
{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

}
-(void)timerFired
{
    if((currHour>0 || currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        if(currSeconds==0)
        {

            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {

            currSeconds-=1;
        }
        if(currMinute>-1)


enter code here
            [self.timeonelabel setText:[NSString stringWithFormat:@"%d%@%d%@",currHour,@":",currMinute,@":"]];
        //[self.hourlabel setText:[NSString stringWithFormat:@"%d%@",currHour,@":"]];
          //  [self.timeonelabel setText:[NSString stringWithFormat:@"%d%@%d%@",00,@":",currMinute,@":"]];
        [self.timetwolabel setText:[NSString stringWithFormat:@"%02d",currSeconds]];
    }
    else
    {
        [self.timer invalidate];
    }
}

On passing the current time and end time the timer should run to countdown to 0 .Problem is timer is not running to count down, Please help. I am even stuck in displaying the timer count down in tableview..please help


回答1:


You have a couple of thing wrong in your code regarding creation of date from a string. First of all, your specified date format in the NSDateFormatter and the actual format you use don't match - one has seconds and the other doesn't. This means that dateFromString will return nil. Also, you should also specify a date in stead of only time since you otherwise can't be sure that you will get today's date.

Finally, you are overcomplicating things by extracting and keeping track of all of components of NSDate in primitive variables. I would rather do something like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"YYYY/MM/dd HH:mm"];
    self.endTime = [df dateFromString:@"2016/02/23 11:00"];

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}

-(void)timerFired {
    if ([[NSDate date] compare:self.endTime] == NSOrderedAscending) {
        NSLog(@"still going");
        // enter code here
        // Use NSDateComponentsFormatter to show what you need to show
    }
    else {
        NSLog(@"done");
        [self.timer invalidate];
    }
}



回答2:


try this code  maybe this is your requirement 


- (void)viewDidLoad
{
    NSCalendar *g = [[ NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];


    NSDateComponents *d_comp1 = [[NSDateComponents alloc]init];
     // lower value for  time  compare to  d_comp time
    [d_comp1 setHour: 1]; 
    [d_comp1 setMinute: 1]; 
    [d_comp1 setSecond: 00];


    NSDateComponents *d_comp = [[NSDateComponents alloc]init];
    enter code here
    [d_comp setHour: 10];
    [d_comp setMinute: 1];
    [d_comp setSecond: 10];


    NSDate *date1 = [g dateFromComponents: d_comp1];

    NSDate *date2 = [g  dateFromComponents: d_comp];


            NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
        int hours = (int)interval / 3600;             // integer division to get the hours part
        int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
        int sec = (int)d_comp1.second - (int)d_comp.second;
    if (sec<0) {
        sec+=60;

    }
        currHour=hours;
        currMinute=minutes;
        currSeconds= sec;
        [self start];
}

-(void)start
{
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

}
 -(void)timerFired
{
        if(currHour>0 || currMinute>0 || currSeconds >0)
        {

            if (currSeconds < 0) {
                currSeconds = 60;
                currMinute -= 1;
            }
            if (currMinute < 0) {
                currHour -= 1;
                currMinute = 59;
                currSeconds =60;
            }
            [self.timeonelabel  setText:[NSString stringWithFormat:@"%d:%d:%d",currHour,currMinute,currSeconds]];
            currSeconds--;
        }
        else
        {
            [self.timeonelabel  setText:[NSString stringWithFormat:@"%d:%d:%d",currHour,currMinute,currSeconds]];

            [timer invalidate];
        }
}


来源:https://stackoverflow.com/questions/35573340/how-to-countdown-timer-on-hours-and-mins-and-stop-when-its-0-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!