2011-10-21 130 views
0

我是新来的iPhone NSTimer。我做了一个秒表。现在我想暂停 之间的时间,也想恢复暂停。我的代码给出在下面。暂停在iPhone中停止计时计时器?

  • 我该如何暂停时间?
  • 我该如何从时间停止的地方恢复?

开始时间:

startDate = [[NSDate date]retain]; 
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 
                 target:self 
                selector:@selector(updateTimer) 
                userInfo:nil 
                repeats:YES]; 

停产时间:

[stopWatchTimer invalidate]; 
stopWatchTimer = nil; 
[self updateTimer]; 

更新时间:

(void)updateTimer 
{ 
    NSDate *currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"mm:ss"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    NSString *timeString=[dateFormatter stringFromDate:timerDate]; 
    stopWatchLabel.text = timeString; 
    [dateFormatter release]; 
} 

回答

1

NSTimer没有暂停或恢复的方法。您可以制作两种类型的定时器,一种仅实现一次,另一种则重复。例如:

创建一个计时器,每秒钟会进入回调myMethod。

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 
    target:self 
    selector:@selector(myMethod:) 
    userInfo:nil 
    repeats:YES]; 

你可能会选择这一个你的目的凡在你的类,你应该保持一定的

BOOL pausevariable和回调的MyMethod做到以下几点:

- (void) myMethod:(NSTimer *) aTimer 
{ 
    if (!pause) { 
     // do something 
     // update your GUI 
    } 
} 

,你在更新暂停你的代码。要停止计时器并释放内存,请致电

[myTimer invalidate]; 

希望这对我有所帮助。

+0

我知道的NSTimer没有暂停和恢复方法。但我不知道如何暂停,所以有什么逻辑来暂停计时器。顺便谢谢你的夸奖。 – Rishabh

+2

那么你为什么说“我是NSTimer的新手,我做了一个秒表,现在我想暂停时间,也想暂停恢复”。 :) – Gabe

+0

但我想在我的程序加入暂停按钮等等...... – Rishabh

3

我创造了一些额外的变量,以帮助保持轨道上的经过时间:

  1. BOOL watchStart;
  2. NSTimeInterval pauseTimeInterval;

我设置watchStart = NO和pauseTimeInterval = 0.0。

我用watchStart检查其开始或暂停状态(我IBAction),并在updateTimer,我设置了timeIntervalpauseTimeInterval

代码的关键部分是:

_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))] 

该行将增加额外的timeIntervalstartDate对于该暂停的时间计算。这有点像“退后”,以便在先前记录的时间间隔中添加。

-(void) updateTimer { 

    NSDate *currentDate = [NSDate date]; 

    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:_startDate];  
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"mm:ss.S"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 

    NSString *timeString = [dateFormatter stringFromDate:timerDate]; 
    _stopWatchLabel.text = timeString; 
    _pauseTimeInterval = timeInterval; 

} 



-(IBAction)btnStartPause:(id)sender { 

    if(_watchStart == NO) { 
     _watchStart = YES; 
     _startDate = [NSDate date]; 
     _startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]; 
     _stopWatchTime = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 
    } 
    else { 
     _watchStart = NO; 
     [_stopWatchTime invalidate]; 
     _stopWatchTime = nil; 
     [self updateTimer]; 
    } 
0

小修正上面的例子:

_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]; 

添加保留@结尾:

_startDate = [[_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]retain]; 

这将使事情:-)

5

您可以使用NSTimeInterval更好而不是定时器。我有一个功能性代码来暂停和停止计时器。

@interface PerformBenchmarksViewController() { 

    int currMinute; 
    int currSecond; 
    int currHour; 
    int mins; 
    NSDate *startDate; 
    NSTimeInterval secondsAlreadyRun; 
} 

@end 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    running = false; 
} 

- (IBAction)StartTimer:(id)sender { 

    if(running == false) { 

     //start timer 
     running = true; 
     startDate = [[NSDate alloc] init]; 
     startTime = [NSDate timeIntervalSinceReferenceDate]; 
     [sender setTitle:@"Pause" forState:UIControlStateNormal]; 
     [self updateTime]; 
    } 
    else { 
     //pause timer 
     secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate]; 
     startDate = [[NSDate alloc] init]; 
     [sender setTitle:@"Start" forState:UIControlStateNormal]; 
     running = false; 
    } 
} 

- (void)updateTime { 

    if(running == false) return; 

    //calculate elapsed time 
    NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate]; 
    NSTimeInterval elapsed = secondsAlreadyRun + currentTime - startTime; 

    // extract out the minutes, seconds, and hours of seconds from elapsed time: 
    int hours = (int)(mins/60.0); 
    elapsed -= hours * 60; 
    mins = (int)(elapsed/60.0); 
    elapsed -= mins * 60; 
    int secs = (int) (elapsed); 

    //update our lable using the format of 00:00:00 
    timerLabel.text = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, mins, secs]; 

    //call uptadeTime again after 1 second 
    [self performSelector:@selector(updateTime) withObject:self afterDelay:1]; 
} 

希望这会有所帮助。由于

+0

有用的,易于理解,解决我的问题,谢谢你的分享!但是,当从经过时间中提取小时数时,我认为它应该是: 'int hours =(int)(elapsed/3600.00);经过 - =小时* 3600;' 否则在60分钟后出错。 – Alison

0
-(void)startStopwatch 
{ 

//initialize the timer HUD 
[self setSeconds:second]; 
// [self.hud.stopwatch setSeconds:_secondsLeft]; 

//schedule a new timer 
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
              target:self 
             selector:@selector(tick:) 
             userInfo:nil 
             repeats:YES]; 
} 

-(void)pause{ 

[_timer invalidate]; 
} 

-(void)resume { 

[_timer isValid]; 

} 

使用此代码,我们可以暂停和恢复定时器

0

朋友,我成功地实施暂停和恢复功能的iOS定时器应用,代码如下

- (IBAction)pauseT:(id)sender { 
se = sec; 
mi = min; 
ho = hour; 
[timer invalidate]; 

}

- (IBAction)resumeT:(id)sender { 
sec = se; 
min = mi; 
hour = ho; 
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES]; 

}

希望这对你的作品家伙..