0

イベントへのカウントダウンを表す int 値があります (ビューが表示されます)。現在、私はこれを行います:

- (void) buttonTapped:(id)sender {
    [self performSelector:@selector(displayView) afterDelay:countdownValue];
}

私がやりたいことは次のとおりです...

ボタンがタップされると、カウントダウンが終了するまでナビゲーション バーの色合いを赤、黄、緑に比例して変更し、ビューを表示します。

たとえば、カウントダウンが 3 秒の場合、各色に 1 秒、5 秒の場合、各色に 1.666 です。

これをスケジュールするために NSTimer を使用できますか?

ビューが表示された後、タイマーを無効にする必要があります。

ありがとう

4

1 に答える 1

3
float count_down = 5.0;
[NSTimer scheduledTimerWithTimeInterval: count_down*(1.0/3.0) target: self
                               selector: @selector(changeToRed:) userInfo: nil repeats: NO];
[NSTimer scheduledTimerWithTimeInterval: count_down*(2.0/3.0) target: self
                               selector: @selector(changeToGreen:) userInfo: nil repeats: NO];
[NSTimer scheduledTimerWithTimeInterval: count_down target: self
                               selector: @selector(changeToBlue:) userInfo: nil repeats: NO];

そしてコールバック(そこでやりたいことは何でもします):

-(void) changeToRed:(NSTimer*) t {
    NSLog(@"red");
}

-(void) changeToGreen:(NSTimer*) t {
    NSLog(@"green");
}

-(void) changeToBlue:(NSTimer*) t {
    NSLog(@"blue");
}
于 2011-02-25T16:07:54.133 に答える