我正在尝试使用CABasicAnimation创建一个简单的动画,该动画将始终保持恒定的速度,即使动画必须经过的距离永远都不相同。到目前为止,这是我的代码,它正在滚动一个标签,该标签的大小始终会变化,但仍无法保持恒定的速度。非常感谢您的帮助。

CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.speed = 1.0f;

NSNumber *fromValue = [NSNumber numberWithFloat:self.mainLabel.frame.origin.x];
NSNumber *toValue = [NSNumber numberWithFloat:-self.mainLabel.frame.size.width - self.view.frame.size.height];
theAnimation.fromValue = fromValue;
theAnimation.toValue = toValue;
//theAnimation.duration = toValue.floatValue - fromValue.floatValue;//Not right.
theAnimation.repeatCount = 999;
theAnimation.autoreverses = NO;
[mainLabel.layer addAnimation:theAnimation forKey:@"animateLayer"];

最佳答案

您是否尝试添加:

 theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

在之后的例子中


此功能设置动画的线性速度。

08-19 12:19