2013-06-24 12 views
4

我开发了一个应用程序,它需要车轮绕z轴旋转,随着时间的推移逐渐增加或降低车轮速度。我使用CABasicAnimation &我的代码如下。虽然我在特定的时间间隔更改了层的速度属性,但会导致车轮产生“干扰”效应。在旋转车轮时改变CALayer在CABasicAnimation中的速度引起混乱效应

/* ** */

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(); 
/************/ 

在定时器予改变的ImageView的CALayer的速度如下,其中dPlayedPercentage是一个变量。

imageLeft.layer.speed=1.0+dPlayedPercentage; 

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

我认为这是由于位置复位而改变的CALayer的速度性能。我应该怎样做才能纠正这一点。或者有任何其他方式来做这个动画?

+0

添加以下代码已整流的加加速度在动画中。 imageLeft.layer.timeOffset = [imageLeft.layer convertTime:CACurrentMediaTime()fromLayer:nil]; \t imageLeft.layer.beginTime = CACurrentMediaTime(); \t imageLeft.layer.speed = 1.0 + dPlayedPercentage; – Augus

回答

9

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

imageLeft.layer.timeOffset = [imageLeft.layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
imageLeft.layer.beginTime = CACurrentMediaTime(); 
imageLeft.layer.speed=1.0+dPlayedPercentage; 
+0

+1我几乎有同样的问题,有一个动画层,我想改变速度中动画,而不重新启动动画。这对我有用,谢谢。 – Siegfoult

+0

@Augus @Siegfoult我有类似的问题,你能告诉我你如何管理'dPlayedPercentage'价值。 –

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