-3

10 から 0 までのカウントダウン タイマーを作成します。カウンターを秒単位で表示する uilabel を作成します。今度は、00:00 のように、ラベルにカウンターの分と秒を表示させたいと思います。どうやってやるの?ここに私のカウントダウンコードがあります:

-(void)countdown
{
    countdownCounter -= 1;
    countdown.text = [NSString stringWithFormat:@"%i", countdownCounter];

}

-(IBAction)strat:(id)sender
{
    countdownCounter = 10;
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(countdown) userInfo:nil repeats:YES];
    }

}

ありがとう!

4

4 に答える 4

3

これは、1 回で 1 つのラベルを使用して実行できます。次のコードを使用してみてください。

int seconds = [countDown.text intValue] % 60; 
int minutes = ([countDown.text intValue] / 60) % 60; 
countDown.text = [NSString stringWithFormat:@"%2d:%02d", minutes, seconds]; 
于 2012-06-25T20:20:49.893 に答える
2

まったく同じ方法で、別のタイマーを追加するだけです。

  countdownTimer2 = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(countdown2) userInfo:nil repeats:YES];

-(void)countdown2
{
    countdownCounterMinutes -= 1;
}

and change countdown to

-(void)countdown
{
    countdownCounter -= 1;
    countdown.text = [NSString stringWithFormat:@"%i%i", countdownCounterMinutes, countdownCounter];

}
于 2012-06-25T18:13:07.240 に答える
1

最終的に答えを知っている人のために、私はこのようにします:

-(IBAction)start
{
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self     selector:@selector(updateTimer:) userInfo:nil repeats:YES];
}

-(void)updateTimer:(NSTimer *)timer {
    currentTime -= 10 ;
    [self populateLabelwithTime:currentTime];
    if(currentTime <=0)
       [timer invalidate];
}


- (void)populateLabelwithTime:(int)milliseconds {
    seconds = milliseconds/1000;
    minutes = seconds / 60;
    hours = minutes / 60;

    seconds -= minutes * 60;
    minutes -= hours * 60;

    NSString * result1 = [NSString stringWithFormat:@"%@%02d:%02d:%02d:%02d", (milliseconds<0?@"-":@""), hours, minutes, seconds,milliseconds%1000];
    result.text = result1;

}

viewDidLoad で、ミリ秒単位のカウントダウン時間に currentTime を設定しました。ご理解いただければ幸いです...

于 2012-06-26T17:05:23.753 に答える