3

こんにちは、秒単位のカウントダウンを既に作成しています。たとえば、10 秒としますが、より正確にしたいと考えています。

10.0 に変換してラベルに表示するにはどうすればよいですか? 前もって感謝します

これは、「2番目の」カウントダウンのために私が今持っているものです

私のNSTimer

counterSecond = 10
NSTimer timer1 = [NSTimer scheduledTimerWithTimeInterval : 1
    Target:self selector:@selector (countLabel) userInfo:nil repeats:YES];



-(void)countLabel:
counterSecond --;

self.timerLabel.text = [NSString stringWithFormat @"%d", counterSecond];
4

2 に答える 2

15

カウントダウンを追跡するために開始日時を使用します。iOSは、他のタスクのタイマーの起動を遅らせることができるためです。

- (void)countdownUpdateMethod:(NSTimer*)theTimer {
    // code is written so one can see everything that is happening
    // I am sure, some people would combine a few of the lines together
    NSDate *currentDate = [NSDate date];
    NSTimeInterval elaspedTime = [currentDate timeIntervalSinceDate:startTime];

    NSTimeInterval difference = countdownSeconds - elaspedTime;
    if (difference <= 0) {
        [theTimer invalidate];  // kill the timer
        [startTime release];    // release the start time we don't need it anymore
        difference = 0;         // set to zero just in case iOS fired the timer late
        // play a sound asynchronously if you like
    }

    // update the label with the remainding seconds
    countdownLabel.text = [NSString stringWithFormat:@"Seconds: %.1f", difference];
}

- (IBAction)startCountdown {
    countdownSeconds = 10;  // Set this to whatever you want
    startTime = [[NSDate date] retain];

    // update the label
    countdownLabel.text = [NSString stringWithFormat:@"Seconds: %.1f", countdownSeconds];

    // create the timer, hold a reference to the timer if we want to cancel ahead of countdown
    // in this example, I don't need it
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector     (countdownUpdateMethod:) userInfo:nil repeats:YES];

    // couple of points:
    // 1. we have to invalidate the timer if we the view unloads before the end
    // 2. also release the NSDate if don't reach the end
}
于 2011-04-09T00:38:36.977 に答える
0
counterSecond = 10
NSTimer timer1 = [NSTimer scheduledTimerWithTimeInterval : 0.1
    Target:self selector:@selector (countLabel) userInfo:nil repeats:YES];



-(void)countLabel:
counterSecond - 0.1;

self.timerLabel.text = [NSString stringWithFormat @"%f", counterSecond];

それはうまくいくはずです。

于 2011-04-08T19:41:25.310 に答える