繁体   English   中英

如何计算倒计时?

[英]how to count down time?

我想从10到0倒数时间,并通过此代码来做到这一点..但它不能正常工作

-(void)elapsedTime

{

    static int i = 11;

    UILabel *label;

    label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];

    label.textColor = [UIColor redColor];

    label.font = [UIFont boldSystemFontOfSize:25.0f];

    label.backgroundColor=[UIColor blackColor];

    label.text = [NSString stringWithFormat:@"%i",i];

    [self.view addSubview:label]; 
    i--;
    if(i<0)
    {
        [aTimer invalidate];
        aTimer = nil;
    }
    [label sendSubviewToBack:self.view];
}

为此,您应该使用NSTimer

在.h文件中

{
NSTimer *timer
}

在.m文件中放这个

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

现在添加一个函数,它将每1秒调用一次。 您可以在此功能中更新标签。

-(void)updateLabel {
    if (timeCount==0) {
        [timer invalidate];

    } else {
        timeCount = timeCount-1;
        label.text = [NSString stringWithFormat:@"%d",timeCount];
    }
}

希望这可以帮助。

对您的应用使用以下代码。 并从viewDidload调用addlabel并在.h文件中定义label和i。

- (无效)addlabel {

i=10;

label = [[UILabel alloc] initWithFrame:CGRectMake(150, 27, 50, 50)];

label.textColor = [UIColor redColor];

label.font = [UIFont boldSystemFontOfSize:25.0f];

label.backgroundColor=[UIColor blackColor];

label.text = [NSString stringWithFormat:@"%i",i];

[self.view addSubview:label];

aTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(elapsedTime) userInfo:nil repeats:YES];

}

- (无效)elapsedTime {

i--;
label.text = [NSString stringWithFormat:@"%i",i];
if(i<1)
{
    [aTimer invalidate];
    aTimer = nil;
}

}

尝试这个

。H

            NSTimer* time;
            int count;

.m文件

    viedidload
    count=10;

   time=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeShedule) userInfo:nil repeats:YES];

    -(void)timeShedule {
        label.text=[NSString stringWithFormat:@"%d",count];
         count--;
        if (count==0) {
           [time invalidate];
            time=nil;
       }
    }

在.h中

UILabel *label;

在.m中

- (void)viewDidLoad
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(850, 27, 50, 50)];
    label.textColor = [UIColor redColor];
    label.font = [UIFont boldSystemFontOfSize:25.0f];
    label.backgroundColor=[UIColor blackColor];
    label.text = @"10";
    [self.view addSubview:label]; 

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(elapsedTime:) userInfo:nil repeats:YES];
}

- (void)elapsedTime:(NSTimer *)timer
{
    int currentValue = [label.text intValue];
    currentValue--;
    label.text = [NSString stringWithFormat:@"%i",currentValue];

    if (currentValue == 0)
    {
        [self.view sendSubviewToBack:label];
        [timer invalidate];
        timer = nil;
    }
}

如果您创建一些计数器应用程序,例如http://download.cnet.com/Work-Time-Counter/3000-2124_4-75903089.html

您需要使用NSDate,如下所示:

-(void) start {
   // Save start time
   self.started = [[NSDate alloc] init];
   self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                              target:self
                                            selector:@selector(updateCallback)
                                            userInfo:nil
                                             repeats:YES];
}

-(void)updateCallback {
   // Count time since start and notify UI
   [self.callback counterTick:[ self.started timeIntervalSinceNow ]];  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM