简体   繁体   English

暂停并恢复NSTimer

[英]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- 我在程序中使用NSTimer播放视频,NSTimer连续运行以在函数中暂停和恢复NSTimer的方式运行,我的动作是-

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

There is no pause and resume functionality in NSTimer. 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]; 执行动作时,您可以只使用[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. 您不能暂停计时器。可以保存计时器的fireDate以及当前日期。 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). 当需要恢复计时器时,可以创建一个新的计时器对象,并将触发日期设置为旧的触发日期加上用户在菜单中的时间(oldTime + currentTime)。

Refer how-to-pause-and-resume-nstimer-in-iphone link 请参阅如何在iPhone中暂停和恢复nstimer链接

- (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 您可以在这里找到它: https : //github.com/LianaChu/LCPausableTimer

It works very similarly to BornCoder's answer. 它的工作原理与BornCoder的答案非常相似。 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. 请不要犹豫,在这里回复您的评论,或在github页面的问题选项卡上发布。

Please see the link below 请看下面的链接

How can I programmatically pause an NSTimer? 如何以编程方式暂停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. 您可以存储自计时器启动以来已过去的时间...计时器启动时,将日期存储在NSDate变量中。 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. 然后,当用户切换时...使用NSDate类中的timeIntervalSinceNow方法存储已过的时间...请注意,这将为timeIntervalSinceNow提供一个负值。 When the user returns use that value to set an appropriate timer. 用户返回时,使用该值设置适当的计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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