0

私は使っている

[NSTimer scheduledTimerWithTimeInterval: _callbackPeriod
                                 target: self
                               selector: @selector(timerCallback:)
                               userInfo: nil
                                repeats: NO];

間隔を計る。この間隔は異なる場合がありますが、1 秒でテストしています。間隔 (1 秒) ごとにシンプルな UI テキスト ボックスが更新され、タイマーが再びスケジュールされます。更新は次の方法で呼び出されます

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
NSNotification *notification = [NSNotification notificationWithName:named 
                                                             object:info];
[notificationCenter postNotification:notification];

通知コードが実行します

NSString* timerString = [NSString stringWithFormat:@"%i", info.timerCount];
[_timerValue setStringValue:timerString];

どこ

@property (weak) IBOutlet NSTextField *timerValue;

このメソッドは、間隔が終了するたびにインクリメントされる実行中のカウンターを表示します。

私が抱えている問題は、データの表示が遅いことです。間隔が進むにつれてスムーズに表示されるはずですが、途切れ途切れになっています。NSLogs は、実際にはデータが滑らかであることを示していますが、表示はそうではありません。したがって、1,2,3,4 5 などを表示する代わりに、1,3, 4, 6,... と表示されます。setStringValue の周りに砂糖が必要ですか?

ありがとう。

4

1 に答える 1

0

カウントダウン タイマーの例を次に示します。

- (void)startCountdown {
    countdownSeconds = 60; 
    startTime = [NSDate date];

    countdown = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
    selector:@selector(countdownUpdateMethod:) userInfo:nil repeats:YES];

    // invalidate the timer if the view unloads before the end
    // also release the NSDate if it does not reach the end
}

- (void)countdownUpdateMethod:(NSTimer*)theTimer {

    NSDate *currentDate = [NSDate date];
    NSTimeInterval elaspedTime = [currentDate timeIntervalSinceDate:startTime];

    NSTimeInterval difference = countdownSeconds - elaspedTime;
    if (difference <= 0) {
        [theTimer invalidate];  // kill the timer
        difference = 0;
    }

    // Update the label with the remaining seconds
    NSString *countdownString = [NSString stringWithFormat:@"%f",difference];

    countdownLabel.text = [countdownString substringToIndex:2];
   // NSLog(@"%f",difference);
}
于 2013-05-08T23:55:45.147 に答える