私は自分のプログラムで NSTimer を使用してビデオを再生しています。NSTimer を継続的に実行して関数で実行し、NSTimer を一時停止して再開する方法を実行しています。私のアクションは-
-(void)handleTap
{
if(pause==YES)
{
//resume ;
}
else
{
//pause;
}
}
NSTimer には、一時停止と再開の機能はありません。以下のコードのようにすることができます。
- (void)startTimer {
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)fireTimer:(NSTimer *)inTimer {
// Timer is fired.
}
- (void)resumeTimer {
if(m_pTimerObject) {
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)pauseTimer {
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
このコード スニペットがお役に立てば幸いです。
you can't pause a timer.
you can either:
- (IBAction)pauseResumeTimer:(id)sender {
if (timerRunning == NO) {
timerRunning = YES;
[pauseTimerBtn setTitle:@"Resume" forState:UIControlStateNormal];
NSString *stringVal = [NSString stringWithFormat:@"%@",timeTxt.text];
stringVal = [stringVal stringByReplacingOccurrencesOfString:@":" withString:@"."];
float tempFloatVal = [stringVal floatValue];
int minuteValue = floorf(tempFloatVal);
float tempSecVal = [stringVal floatValue] - floorf(tempFloatVal);
int secondVal = tempSecVal*100;
minuteValue = minuteValue*60;
oldTimeValue = minuteValue + secondVal;
[timer invalidate];
timer = nil;
}
else
{
timerRunning = NO;
[pauseTimerBtn setTitle:@"Pause" forState:UIControlStateNormal];
startDate = [NSDate date];
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}
}
- (void)runTimer:(NSTimer *)timer {
NSInteger secondsAtStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
secondsAtStart = secondsAtStart + oldTimeValue;
NSInteger seconds = secondsAtStart % 60;
NSInteger minutes = (secondsAtStart / 60) % 60;
NSInteger hours = secondsAtStart / (60 * 60);
NSString *result = nil;
result = [NSString stringWithFormat:@"%02ld:%02ld",(long)minutes,(long)seconds];
timeTxt.text = result;
}
タイマーの一時停止と一時停止解除を処理できるコントローラー クラスを作成しました。ここで見つけることができます: https://github.com/LianaChu/LCPausableTimer
これは、BornCoder の回答と非常によく似ています。このコントローラー クラスは、プロジェクトでタイマーを頻繁に一時停止および一時停止解除する必要がある場合に役立ちます。
使用方法、変更点、追加してほしい機能など、コメントやフィードバックをお寄せいただければ幸いです。ここでコメントを返信するか、github ページの問題タブに投稿してください。
There is no specific method in nstimer to pause and resume .You need to invalidate the timer if u want to stop the timer
On performing action you can just use [timer invalidate];
and when you want to resume you just start again your timer
タイマーを一時停止することはできません。タイマーの fireDate と現在の日付を保存できます。この後、タイマーを無効にします。タイマーを再開する必要がある場合は、新しいタイマー オブジェクトを作成し、起動日を古い起動日にユーザーがメニューにいた時間 (oldTime + currentTime) に設定します。
以下のリンクをご覧ください
プログラムで NSTimer を一時停止するにはどうすればよいですか?
私はリンゴでこの答えを見つけました
タイマーが開始してから経過した時間を保存できます... タイマーが開始すると、日付が NSDate 変数に保存されます。次に、ユーザーが切り替えると... NSDateクラスのtimeIntervalSinceNowメソッドを使用して、経過した時間を保存します...これにより、timeIntervalSinceNowに負の値が与えられることに注意してください。ユーザーが戻ってきたら、その値を使用して適切なタイマーを設定します。