1

我想从 10 倒计时到 0。并且 em 用这段代码来做这件事。但它不能正常工作

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

6 回答 6

1

你应该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];
    }
}

希望这可以帮助。

于 2013-05-30T09:41:32.670 回答
0

尝试这个

。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;
       }
    }
于 2013-05-30T09:44:41.790 回答
0

如果您创建一些计数器应用程序,例如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 ]];  
}
于 2014-02-07T21:06:28.767 回答
0

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

-(无效)添加标签{

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

}

-(无效)经过时间{

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

}

于 2013-05-30T09:41:12.917 回答
0

在.h

UILabel *label;

- (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;
    }
}
于 2013-05-30T09:47:38.097 回答