如何在小时和分钟倒计时并在 0 时停止,目标 c [英] How to countdown timer on hours and mins and stop when it's 0 , objective c

查看:22
本文介绍了如何在小时和分钟倒计时并在 0 时停止,目标 c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在传递当前时间和结束时间时,计时器应该运行到 0 倒计时.问题是计时器没有运行倒计时,请帮忙.我什至无法在 tableview 中显示计时器倒计时..请帮忙

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

推荐答案

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

这篇关于如何在小时和分钟倒计时并在 0 时停止,目标 c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆