繁体   English   中英

暂停并恢复NSTimer

[英]pause and resume NSTimer

我在程序中使用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;
}

希望这段代码片段对您有所帮助。

您不能暂停计时器。

您可以:

  • 使计时器无效并在要恢复时创建另一个计时器(在大多数情况下更可取)
  • 或在程序中设置一个标志/变量以注意计时器操作应被忽略。

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

执行动作时,您可以只使用[timer invalidate]; 而当您想恢复时,只需重新启动计时器

您不能暂停计时器。可以保存计时器的fireDate以及当前日期。 此后,您使计时器无效。 当需要恢复计时器时,可以创建一个新的计时器对象,并将触发日期设置为旧的触发日期加上用户在菜单中的时间(oldTime + currentTime)。

请参阅如何在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;

}

我写了一个控制器类,可以处理暂停和取消暂停的计时器。 您可以在这里找到它: https : //github.com/LianaChu/LCPausableTimer

它的工作原理与BornCoder的答案非常相似。 如果您发现自己需要经常在项目中暂停和取消暂停计时器,则此控制器类可能很有用。

如果您使用它,我将不胜感激任何评论或反馈,例如您的使用方式,您希望看到的更改或希望我添加的任何功能。 请不要犹豫,在这里回复您的评论,或在github页面的问题选项卡上发布。

请看下面的链接

如何以编程方式暂停NSTimer?

我在苹果上找到了这个答案

您可以存储自计时器启动以来已过去的时间...计时器启动时,将日期存储在NSDate变量中。 然后,当用户切换时...使用NSDate类中的timeIntervalSinceNow方法存储已过的时间...请注意,这将为timeIntervalSinceNow提供一个负值。 用户返回时,使用该值设置适当的计时器。

暂无
暂无

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

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