1

私は自分のプログラムで NSTimer を使用してビデオを再生しています。NSTimer を継続的に実行して関数で実行し、NSTimer を一時停止して再開する方法を実行しています。私のアクションは-

-(void)handleTap
{
    if(pause==YES)
    {
      //resume ;
    }
    else
    {
      //pause;
    }
}
4

8 に答える 8

4

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;
}

このコード スニペットがお役に立てば幸いです。

于 2012-08-29T06:32:25.530 に答える
2

you can't pause a timer.

you can either:

  • invalidate the timer and create another when you want to resume (preferable in most scenarios)
  • or set a flag/variable in your program to note that timer actions should be ignored.
于 2012-08-29T06:29:56.867 に答える
0
- (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;

}
于 2014-03-13T09:28:47.803 に答える
0

タイマーの一時停止と一時停止解除を処理できるコントローラー クラスを作成しました。ここで見つけることができます: https://github.com/LianaChu/LCPausableTimer

これは、BornCoder の回答と非常によく似ています。このコントローラー クラスは、プロジェクトでタイマーを頻繁に一時停止および一時停止解除する必要がある場合に役立ちます。

使用方法、変更点、追加してほしい機能など、コメントやフィードバックをお寄せいただければ幸いです。ここでコメントを返信するか、github ページの問題タブに投稿してください。

于 2015-05-14T22:34:14.717 に答える
0

There is no specific method in nstimer to pause and resume .You need to invalidate the timer if u want to stop the timer

于 2012-08-29T06:29:22.673 に答える
0

On performing action you can just use [timer invalidate]; and when you want to resume you just start again your timer

于 2012-08-29T06:30:10.527 に答える
0

タイマーを一時停止することはできません。タイマーの fireDate と現在の日付を保存できます。この後、タイマーを無効にします。タイマーを再開する必要がある場合は、新しいタイマー オブジェクトを作成し、起動日を古い起動日にユーザーがメニューにいた時間 (oldTime + currentTime) に設定します。

How-to-pause-and-resume-nstimer-in-iphoneリンクを参照

于 2012-08-29T06:33:56.370 に答える
-1

以下のリンクをご覧ください

プログラムで NSTimer を一時停止するにはどうすればよいですか?

私はリンゴでこの答えを見つけました

タイマーが開始してから経過した時間を保存できます... タイマーが開始すると、日付が NSDate 変数に保存されます。次に、ユーザーが切り替えると... NSDateクラスのtimeIntervalSinceNowメソッドを使用して、経過した時間を保存します...これにより、timeIntervalSinceNowに負の値が与えられることに注意してください。ユーザーが戻ってきたら、その値を使用して適切なタイマーを設定します。

于 2012-08-29T06:31:31.753 に答える