2012-08-29 145 views
1

我使用的NSTimer在我的節目播放視頻,的NSTimer連續運行在功能 跑我是如何暫停&簡歷的NSTimer在行動,我的動作是 -暫停和恢復的NSTimer

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

此代碼不足以讓我們猜測,您用來播放視頻的內容! – Dhruv

回答

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

我希望這段代碼片段能幫助你。

+0

感謝您的幫助,對我來說非常有用 – NANNAV

0

在nstimer中沒有具體的方法來暫停和恢復。如​​果你想停止定時器,你需要使定時器無效。

2

你不能暫停定時器。

,您可以:

  • 無效計時器,並創建另一個當你想恢復(在大多數情況下最好)
  • 或在程序中設置一個標誌/變量需要注意的是定時器動作應忽略。
0

在表演,你可以只使用[timer invalidate];動作,當你要恢復你剛開始再次你的計時器

-1

請參考下面

How can I programmatically pause an NSTimer?

的鏈接,我發現這個答案的蘋果

您可以存儲自定時器以來的時間量開始...當計時器開始將日期存儲在NSDate變量中時。 然後,當用戶切換...使用 中的方法timeIntervalSinceNow NSDate類來存儲已經過了多少時間...請注意,這個 將給timeIntervalSinceNow一個負值。當用戶 返回使用該值來設置適當的計時器。

0

您不能暫停計時器,您可以保存計時器的fireDate以及當前日期。在此之後,您使計時器無效。當需要恢復計時器時,您將創建一個新的計時器對象,並將啓動日期設置爲舊啓動日期以及用戶在菜單中的時間(oldTime + currentTime)。

參考how-to-pause-and-resume-nstimer-in-iphone鏈接

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; 

} 
0

我寫了一個控制器類,可以處理暫停和取消暫停計時器。你可以在這裏找到它:https://github.com/LianaChu/LCPausableTimer

它的作品非常類似於BornCoder的答案。如果您發現自己需要在項目中經常暫停和取消暫停定時器,則此控制器類可能非常有用。

如果您使用它,我將非常感謝您如何使用它,您希望看到什麼更改或您希望我添加的任何功能等任何評論或反饋。請不要猶豫,在這裏回覆您的意見,或張貼在github頁面的問題標籤上。