NSTimer 之前的平滑动画,但现在有问题

Smooth Animation before NSTimer, But now it's Glitchy

我的屏幕上有一个 UIImageView 永远旋转...我使用下面的代码,我在这里找到它:

- (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;
}

找到了,旋转看起来很顺畅...

我已经使用以下方法添加了倒数计时器:

-(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;
}

自从将这个定时器添加到我的应用程序后,旋转动画真的是块状的,不再是平滑的旋转...

有什么想法可以解决这个问题吗?

为了检查,我删除了计时器并重新运行应用程序,果然,动画又流畅了:(

更新:

经过更多的尝试和错误,似乎是这行代码减慢了进程:CountDownTimer.text = [NSString stringWithFormat:@"%.1f",ticks]; 即。当我不在屏幕上的 screen/update 标签上显示 countDownTimer 文本时),旋转仍然很平滑...

好迷茫...我能做什么?

使用零时长动画制作动画没有意义。这不是 UIView 动画应该如何工作的。我的猜测是那是你问题的根源。根据定义,零持续时间动画将是跳跃的。

您应该在一段时间内动画化您的更改。 (始终使用非零持续时间。)

编辑:

请注意,您的 timerTick 方法效率极低。您在每个 tick 上将相同的图像加载到图像视图中。不要那样做。使用初始图像设置它,然后仅当您的滴答计数达到零时才更改图像。

您应该 CADisplayLink 试一试,它应该适合您的情况。简单易用,教程海量,随便搜一下