NSTimer Alternatives

Discussion in 'iOS Programming' started by adimanic, Jan 3, 2014.

  1. adimanic macrumors newbie

    Jan 3, 2014

    I'm currently creating a quiz app and I'm utilizing a CABasicAnimation to animate the filling of a circle over an arbitrary number of seconds. At the same time I'm attempting to use an NSTimer firing every second to keep a numerical label of the time remaining. This is where I'm running into problems - whenever I do anything other than watch the animation my NSTimer update function begins calling at the wrong times (as much as 0.5 seconds off). I think this is to do with the way NSTimer's work and how they're added to the run loop - but do I have any alternatives to keep my CABasicAnimation and integer label in sync?

    I've tried adding the timer to the run loop using NSRunLoopCommonModes, decreasing my NSTimer interval and comparing the current time to the time since the timer fired and I've tried commenting out as much of my CPU intensive code as possible and haven't yet been able to get them to sync properly. Is there maybe a way to get the progress from the CABasicAnimation itself?

    My timer code:
    - (void)beginQuestion
     self.timer = [NSTimer timerWithTimeInterval:kTimerInterval target:self selector:@selector(timeRemainingDidUpdate) userInfo:nil repeats:YES];
                [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    - (void)timeRemainingDidUpdate {
        self.timeLeft -= kTimerInterval;
        [self updateTimeLeftLabel];
    - (void) updateTimeLeftLabel {
            [self.timeLeftLabel setText:[NSString stringWithFormat:@"%0.0f", self.timeLeft]];
  2. larswik macrumors 68000

    Sep 8, 2006

Share This Page