2013-12-11 24 views
0

我正在創建計時器,格式爲HH:mm,但現在我的問題是暫停計時器並恢復計時。現在,當您暫停計時器時,它會繼續在後臺計數,但可見部分已停止。一旦你點擊恢復按鈕,計時器就會更新到它離開的地方,在暫停多久之前,計時器可能會超出應有的數量。例如,如果計時器計數到5,那麼暫停計時器,現在坐在5,一旦你點擊繼續,計時器可能會從15開始,因爲它已經停止了10秒。下面是我的代碼,我從幾個地方相結合,得到這個工作:即使定時器在視覺上已停止,NSTimer仍會繼續計數

- (void) createTimerPage 
{ 
    _dateFormat = [NSDate date]; 

    // Add pause button 
    UIImage *pause = [UIImage imageNamed:@"pause.png"]; 
    _pauseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _pauseBtn.frame = CGRectMake(151, 17, 32, 32); 
    [_pauseBtn addTarget:self action:@selector(pauseTimer) forControlEvents:UIControlEventTouchUpInside]; 
    [_pauseBtn setBackgroundImage:pause forState:UIControlStateNormal]; 
} 

- (void) startTimer 
{ 
    // Start timer 
    NSInteger secondsSinceStart = (NSInteger) [[NSDate date] timeIntervalSinceDate:_dateFormat]; 
    NSInteger seconds = secondsSinceStart % 60; 
    NSInteger minutes = (secondsSinceStart/60) % 60; 
    NSString *result = nil; 
    result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds]; 
    _timerText.text = result; 
    [_timerBtns addSubview:_pauseBtn]; 
} 

- (void) startPressed 
{ 
    [_time invalidate]; 
    _time = [NSTimer scheduledTimerWithTimeInterval:1.0 
             target:self 
             selector:@selector(startTimer) 
             userInfo:nil 
             repeats:YES]; 
} 

- (void) pauseTimer 
{ 
    if (_time) { 
     // pause timer 
     [_time invalidate]; 
     [_timerBtns addSubview:_playBtn]; 
    } 
} 
+0

看,上面寫着secondsSinceStart =行[NSDate的日期] timeIntervalSinceDate:_dateFormat。當用戶啓動,暫停,然後恢復計時器時,secondsSinceStart的值是多少? – rocky

回答

0

我懷疑的問題是,你是存儲在一個名爲「_dateFormat」變量「開始時間」。當調用createTimerPage時,該變量只會被設置,當然,當您稍後嘗試確定startTimer中的secondsSinceStart時,它仍在計算自調用createTimerPage以來的時間。老實說,對於你想要做的事情,我根本不用擔心開始時間。相反,我只是使用計時器方法被調用次數的計數器。我也會重新命名一些你的方法,使它更清楚實際正在發生的事情。例如,你的「startTimer」方法實際上並沒有啓動一個定時器。當你的秒鐘計時器激活時,它會被調用。

最後,還有一個問題是您不斷添加子視圖,但至少在發佈代碼後,永遠不會刪除它們。您可以(應該)在每次添加新的子視圖時刪除未使用的子視圖。老實說,儘管如此,將兩個視圖作爲視圖層次結構的一部分並使用隱藏屬性僅顯示其中一個視圖會更好。

像這樣的事情會做你想要什麼:

NSUInteger _timerSeconds; 
NSTimer * _timer; 

- (void) createTimerPage 
{ 
    // Add pause button 
    UIImage *pause = [UIImage imageNamed:@"pause.png"]; 
    _pauseBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    _pauseBtn.frame = CGRectMake(151, 17, 32, 32); 
    [_pauseBtn addTarget:self action:@selector(pausePressed) forControlEvents:UIControlEventTouchUpInside]; 
    [_pauseBtn setBackgroundImage:pause forState:UIControlStateNormal]; 
    [_timerBtns addSubview:_pauseBtn]; 
} 

- (void) timerFired 
{ 
    _timerSeconds++; 
    NSInteger seconds = _timerSeconds % 60; 
    NSInteger minutes = (_timerSeconds/60) % 60; 
    NSString * result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds]; 
    _timerText.text = result; 
} 

- (void) startPressed 
{ 
    // start timer 
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
              target:self 
              selector:@selector(timerFired) 
              userInfo:nil 
              repeats:YES]; 

    _playBtn.hidden = YES; 
    _pauseBtn.hidden = NO; 
} 

- (void) pausePressed 
{ 
    if (_timer) { 
     // pause timer 
     [_timer invalidate]; 
     _playBtn.hidden = NO; 
     _pauseBtn.hidden = YES; 
    } 
} 
0

我居然能算出這個一出來對此事的一些更多的研究。我發現了一個很好的例子,幫助我想出了以下內容:

- (IBAction)startRepeatingTimer:(id)sender 
{ 
    // Show feed text while timer is going 
    _feeding = [[UILabel alloc] initWithFrame:CGRectMake(92, 25, 100, 50)]; 
    _feeding.text = @"FEEDING"; 
    _feeding.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; 
    if (_feeding) { 
     _feeding.hidden = NO; 
    } 
    [_timerArea addSubview:_feeding]; 

    // Cancel preexisting timer 
    [_repeatingTimer invalidate]; 

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                target:self 
               selector:@selector(timerTicked:) 
               userInfo:nil repeats:YES]; 
    _repeatingTimer = timer; 
    if (_playBtn.hidden == NO) { 
     _playBtn.hidden = YES; 
     _pauseBtn.hidden = NO; 
    } 

    if (_bWindow.frame.origin.y == 0) { 
     [UIView animateWithDuration:0.3 
        animations:^ { 
         _bWindow.frame = CGRectMake(0, 0, self.view.frame.size.width, 203); 
         _bWindow.frame = CGRectMake(0, -130, self.view.frame.size.width, 203); 
        }]; 
    } 

    [_timerBtns addSubview:_pauseBtn]; 
} 

- (void) timerTicked:(NSTimer *)timer 
{ 
    _currentTimeInSeconds++; 
    _timerText.text = [self targetMethod:_currentTimeInSeconds]; 
} 

- (IBAction)resetTimer:(id)sender 
{ 
    _feeding.hidden = YES; 
    if (_repeatingTimer) { 
     [_repeatingTimer invalidate]; 
    } 
    if (_playBtn.hidden == YES) { 
     _playBtn.hidden = NO; 
     _pauseBtn.hidden = YES; 
    } 
    _currentTimeInSeconds = 0; 
    _timerText.text = [self targetMethod:_currentTimeInSeconds]; 
    if (_bothBtn.hidden == YES) { 
     _bothBtn.hidden = NO; 
     _switchBView.hidden = YES; 
    } 
    if (_rightBtn.hidden == YES) { 
     _rightBtn.hidden = NO; 
     _switchRView.hidden = YES; 
    } 
    if (_leftBtn.hidden == YES) { 
     _leftBtn.hidden = NO; 
     _switchLView.hidden = YES; 
    } 
} 

- (IBAction)pauseRepeatingTimer:(id)sender 
{ 
    [_repeatingTimer invalidate]; 
    if (_playBtn.hidden == YES) { 
     _playBtn.hidden = NO; 
     _pauseBtn.hidden = YES; 
    } 
} 

- (NSString *) targetMethod:(int)totalSeconds 
{ 
    //NSInteger secondsSinceStart = (NSInteger) [[NSDate date] timeIntervalSinceDate:_dateFormat]; 
    int seconds = totalSeconds % 60; 
    int minutes = (totalSeconds/60) % 60; 
    return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds]; 
}