簡體   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