简体   繁体   中英

pause and resume NSTimer

I'm using NSTimer in my program to play video, NSTimer run continuously to run in the function how I'm pause & resume NSTimer in action, my action is-

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

There is no pause and resume functionality in NSTimer. You can do like below mentioned code.

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

I hope this code snippets will help you.

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.

nstimer中没有暂停和恢复的特定方法。如果您想停止计时器,则需要使计时器无效

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

You can't pause a timer.you can save the fireDate of the timer and also the current date. After this you invalidate the timer. When need to resume timer you create a new timer object and set the fire date to the old fire date plus the time the user was in the menu (oldTime + currentTime).

Refer how-to-pause-and-resume-nstimer-in-iphone link

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

}

I wrote a controller class that can handle pausing and unpausing timers. You can find it here: https://github.com/LianaChu/LCPausableTimer

It works very similarly to BornCoder's answer. This controller class can be useful if you find yourself needing to pause and unpause timers very often in your project.

If you use it, I would greatly appreciate any comments or feedback like how you used it, what changes you would like to see, or any features you would like me to add. Please don't hesitate to reply with your comments here, or post on the issues tab on the github page.

Please see the link below

How can I programmatically pause an NSTimer?

I found this answer on the apple

You can store the amount of time that has passed since the timer started... When the timer starts store the date in an NSDate variable. Then when the user switches... use the method timeIntervalSinceNow in the NSDate class to store how much time has passed... note that this will give a negative value for timeIntervalSinceNow. When the user returns use that value to set an appropriate timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM