1

timer1Elapsed がカウントダウン タイマーを終了してから次の timer2Elapsed に進み、カウントダウンが終了したら timer1Elapsed に戻ってカウントダウンする、というように 5 回繰り返します。(A -> B -> A -> B ... 5 回) のように見えます A の状態で、B には固定時間があります。A を 1 分間カウントし、B を 30 秒間カウントするとします。私は初心者なので、タイプミスをしないでください。コードが間違っている場合は修正してください。寛大になってください。

- (void) timer2Elapsed:(id)timer
{
    ...

    if (remainingTime <= 0)
    {
        [timer invalidate];
        [NSTimer scheduledTimerWithTimeInterval:0.99 target:self selector:@selector(timer3Elapsed:) userInfo:nil repeats:YES];
    }

    myDisplay.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

- (void) timer1Elapsed:(id)timer
{
    ...

    if (remainingTime <= 0)
    {
        [timer invalidate];
        [NSTimer scheduledTimerWithTimeInterval:0.99 target:self selector:@selector(timer2Elapsed:) userInfo:nil repeats:YES];
    }

    myDisplay.text = [NSString stringWithFormat:@"%d : %d : %d", hour , minute, second];
}

これは私のボタンです

- (IBAction)startBtn:(id)sender {
        [NSTimer scheduledTimerWithTimeInterval:0.99 target:self selector:@selector(timer1Elapsed:) userInfo:nil repeats:YES];
}
4

1 に答える 1

1

1 つのタイマーを別のタイマーに呼び出す必要がある場合は、次のコードを使用できます。

+ (void) timer2Elapsed:(id)timer
{
    static int numberOfTimes = 0;
    if(numberOfTimes >= 5)
    {
        numberOfTimes = 0;
        return;
    }
    numberOfTimes ++;

    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer1Elapsed:) userInfo:nil repeats:NO];
}

- (void) timer1Elapsed:(id)timer
{
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer2Elapsed:) userInfo:nil repeats:NO];
}

- (void)startTimers
{
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer1Elapsed:) userInfo:nil repeats:NO];
}
于 2012-06-26T10:29:40.373 に答える