NSTimer之前的流畅动画,但现在它是Glitchy

时间:2015-12-02 21:21:46

标签: ios xcode animation rotation nstimer

我的屏幕上永远有一个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];即。当我不在屏幕上的屏幕/更新标签上显示countDownTimer文本时,旋转仍然很顺畅...

如此困惑......我该怎么办?

2 个答案:

答案 0 :(得分:2)

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

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

编辑:

请注意,您的timerTick方法非常低效。在每个刻度线上将相同的图像加载到图像视图中。不要这样做。使用初始图像进行设置,然后仅在刻度计数达到零时更改图像。

答案 1 :(得分:2)

你应该试试CADisplayLink,它应该适合你的情况。它易于使用,有很多教程,只需进行搜索。