0
をクリックします。NSTimerとUIAlertControllerボタン私は単純なコード以下のいる
- (void)showAlert {
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(countdown)
userInfo:nil
repeats:YES];
[UIAlertController showAlertViewWithTitle:@"Title" message:@"" cancelButtonTitle:@"Dismiss" okButtonTitle:nil cancelAction:nil okAction:nil];
}
- (void)countdown {
if(secondsLeft > 0) {
dispatch_async(dispatch_get_main_queue(), ^{
_timerLabel.text = [NSString stringWithFormat:@"%ds", secondsLeft];
});
secondsLeft--;
} else {
[self showTimerLabel:NO];
[countdownTimer invalidate];
}
}
ので、タイマーが警告表示とともに始まりますが、私は、アラートのボタンを押すと、timerLabelテキストの更新と遅延が発生しますが。
たとえば、7秒でボタンを押すと、timerLabelは5秒または4秒にジャンプします。
何が間違っている可能性がありますか?
timerメソッドには 'dispatch_async'は必要ありません。タイマーはすでにメインキューで実行されています。 'NSTimer'は特に正確ではありません。単純なカウンタを減らすのではなく、開始時に 'Date'をとり、' timeIntervalSinceNow'を使って経過時間を取得します。スムーズな更新を確実にするために、1秒ではなく0.1秒でタイマーを実行してください。 – Paulw11