5

我开发了一个应用程序,该应用程序需要一个轮子围绕 z 轴旋转,并随着时间的推移稳定地增加或减少轮子的速度。我使用 CABasicAnimation & 我的代码如下。当我以特定的间隔更改图层的速度属性时,它会导致车轮产生“Jerk”效果。

/ * ** * /

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.toValue = [NSNumber numberWithFloat:-2*M_PI];
animation.duration = 4.0f;
animation.repeatCount = INFINITY;
[animation setValue:@"left" forKey:@"side"];
[animation setDelegate:self];
animation.removedOnCompletion=NO;
animation.fillMode = kCAFillModeForwards;
animation.cumulative = YES;

imageLeft.layer.beginTime = CACurrentMediaTime();
/************/

在计时器中,我如下改变图像视图的 CALayer 的速度,其中 dPlayedPercentage 是一个变量。

imageLeft.layer.speed=1.0+dPlayedPercentage;

[imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"];

我认为这是由于更改 CALayer 的速度属性时位置重置所致。我应该怎么做才能纠正这个问题。或者任何其他方式来做这个动画?

4

2 回答 2

15

添加以下代码已纠正动画中的混蛋。

imageLeft.layer.timeOffset = [imageLeft.layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
imageLeft.layer.beginTime = CACurrentMediaTime(); 
imageLeft.layer.speed=1.0+dPlayedPercentage;
于 2013-08-10T07:43:35.757 回答
0

对于更动态的速度变化,我对先前的答案(根本不绘制图层)有一些问题,因为timeOffset需要在考虑新速度的情况下进行计算。

(来源https://coveller.com/2016/05/core_animation_timing

的基本公式timeOffset是:
timeOffset = CACurrentMediaTime() - ((convertTime - beginTime) x speed)

在代码中:

theLayer.speed = newSpeed

let mediaTime = CACurrentMediaTime()

let converedTime = theLayer.convertTime(mediaTime, to: nil)

theLayer.beginTime = mediaTime

let offset = mediaTime - ((converedTime - theLayer.beginTime) * Double(newSpeed))

theLayer.timeOffset = offset
于 2018-01-04T15:17:18.083 回答