我正在使用以下代码示例来旋转(连续旋转)UIImageView
:
CAKeyframeAnimation *theAnimation = [CAKeyframeAnimation animation];
theAnimation.values = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(0, 0,0,1)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0,0,1)],
[NSValue valueWithCATransform3D:CATransform3DMakeRotation(2*M_PI, 0,0,1)],
nil];
theAnimation.cumulative = YES;
theAnimation.duration = duration;
theAnimation.repeatCount = HUGE_VALF;
theAnimation.removedOnCompletion = YES;
[self.layer addAnimation:theAnimation forKey:@"transform"];
现在我想在显示动画时更改旋转速度(意味着不同的持续时间)。最简单的方法是停止动画并创建一个新动画。这样做的问题是,新动画将从 0 度开始,当旋转 View 的速度发生变化时,这会导致丑陋的跳跃。
如何在不中断动画的情况下更改动画的持续时间?
最佳答案
保存对先前旋转的引用,然后在创建新动画时将其用作起始值。
CALayer* layer = [self.layer presentationLayer];
float currentAngle = [[layer valueForKeyPath:@"transform.rotation.z"] floatValue];
关于为什么你应该调用 [[self.layer presentationLayer] valueForKeyPath ..... 而不是 [self.layer valueForKeyPath ........动画编程指南。由于旋转(变换)已经设置了动画,因此您需要检查所呈现图层的值。
关于objective-c - 如何在不中断动画的情况下更改动画的持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13805710/