Search code examples
iosxcodeanimationrotationnstimer

Smooth Animation before NSTimer, But now it's Glitchy


I have a UIImageView rotating forever on my screen... I use the code below, which I found HERE:

- (void) spinWithOptions: (UIViewAnimationOptions) options {
    [UIView animateWithDuration: 0.0f
                          delay: 0.0f
                        options: options
                     animations: ^{
                         Hand.transform = CGAffineTransformRotate(Hand.transform, M_PI / 30);
                     }
                     completion: ^(BOOL finished) {
                         if (finished) {
                             if (animating) { //if, keep spinning
                                 [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                             } else if (options != UIViewAnimationOptionCurveEaseOut) { //final spin
                                 //[self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                             }
                         }
                     }];
}

- (void) startSpin {
    if (!animating) {
        animating = YES;
        [self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
    }
}

- (void) stopSpin {
    animating = NO;
}

It worked find and the rotation seems smooth...

I've since added a countdown timer, using the follow:

-(void)timerTick:(NSTimer *)timer{
    ticks -= 0.1;   //NSLog(@"Total Ticks: %.2f",ticks);

    if(ticks < 0.0){ //3 seconds is up
        Failed.image = [UIImage imageNamed:@"Failed.png"];
        ticks = 0.0;
        [self GameOver]; //out of time...
    }
    else {
        FailedBeforeTimer.image = [UIImage imageNamed:@"Failed Before.png"];
    }
    CountDownTimer.text = [NSString stringWithFormat:@"%.1f",ticks];
}

-(void)startTimer{
    ticks = 3.0;
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
}

-(void)stopTimer{
    [timer invalidate];
    ticks = 0.0;
}

Since adding this timer to my app, the rotation animation is really blocky and no longer a smooth rotation...

Any ideas what I should implement to solve this?

To check, I removed the timer and re-run the application, and sure enough, the animation was smooth again :(

UPDATE:

After some more trial and error, it seems to be this line of code that slows the process down: CountDownTimer.text = [NSString stringWithFormat:@"%.1f",ticks]; ie. When I don't display the countDownTimer text on the screen/update label on screen), the rotation is smooth still...

So confused... What can I do?


Solution

  • Animating with zero-duration animations does not make sense. That's not how UIView animation is supposed to work. My guess is that is the source of your problems. A zero duration animation will be jumpy by definition.

    You should be animating your changes over some time-period. (Always use a non-zero duration.)

    EDIT:

    Note that your timerTick method is horribly inefficient. You load the same image into an image view on every tick. Don't do that. Set it up with the initial image, and then only change the image when your tick count reaches zero.