秒とミリ秒の単純なカウントダウンを作成したいと思います: SS:MM. しかし、タイマーを止めるか、タイマーが0:00になったら何かをしたいです。現在、タイマーは動作していますが、0:00 で止まりません。秒を止めることはできますが、ミリ秒を止めることはできません。なにが問題ですか?
-(void) setTimer {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = 99;
tmp.secondsCount = 2;
tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
}
-(void) timerRun {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
[tmp.countdownTimerGame invalidate];
tmp.countdownTimerGame = nil;
tmp.lives = tmp.lives - 1;
NSString *newLivesOutput = [NSString stringWithFormat:@"%d", tmp.lives];
livesLabel.text = newLivesOutput;
if (tmp.lives == 0) {
[self performSelector:@selector(stopped) withObject:nil];
}
else {[self setTimer]; }
}
else
tmp.milisecondsCount = 99;
}
NSString *timerOutput = [NSString stringWithFormat:@"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];
timeLabel.text = timerOutput;
}
-(void) stopped {
NSLog(@"Stopped game");
timeLabel.text = @"0:00";
}