首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使当计时器视觉上停止时,NSTimer也一直在计数。

即使当计时器视觉上停止时,NSTimer也一直在计数。
EN

Stack Overflow用户
提问于 2013-12-11 20:40:58
回答 2查看 151关注 0票数 0

我正在创建一个定时器,它的格式是HH:mm,但我现在的问题是暂停定时器并继续它。现在,当您暂停计时器时,它将继续在后台计数,但可见部分已经停止。一旦您单击“简历”按钮,计时器将更新到它停止的位置,等待您暂停多长时间后,定时器可能会比它应该的数量高出任何数字。

例如,如果计时器计数为5,则暂停计时器(现在是5),一旦单击“恢复”,定时器可能在15开始,因为您暂停它已经10秒了。下面是我的代码,我从几个地方组合了这些代码来实现这个功能:

代码语言:javascript
运行
复制
- (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];
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-11 21:46:25

我怀疑问题在于,您正在将“启动时间”存储在名为"_dateFormat“的变量中。这个变量只有在createTimerPage被调用时才会被设置--当然,当您稍后尝试在startTimer中确定secondsSinceStart时,它仍然在计算自调用createTimerPage以来的时间。

老实说,对于你想要做的事情,我根本不会担心开始的时间。相反,我只使用计时器方法被调用的次数的计数器。我还会重命名一些方法,以便更清楚地了解实际发生的事情。例如,您的"startTimer“方法实际上并没有启动计时器。这就是当你的一秒计时器开火时会被调用的东西。

最后,另一个问题是,您正在不断地添加子视图,但是,至少在发布了代码之后,您从未删除它们。每次添加新的子视图时,都可以删除未使用的子视图。不过,老实说,最好将这两个视图保留为视图层次结构的一部分,并使用隐藏属性只显示其中一个视图。

像这样的事情会做你想做的事:

代码语言:javascript
运行
复制
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;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2014-05-16 01:34:20

实际上,通过对这件事进行更多的研究,我就能弄清楚这件事了。我找到了一个很好的例子,帮助我想出了以下几点:

代码语言:javascript
运行
复制
- (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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20529480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档