CABasicAnimation保持恒定的速度

时间:2012-08-16 06:37:00

标签: iphone objective-c ios ipad uilabel

我正在尝试使用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"];

1 个答案:

答案 0 :(得分:1)

您尝试添加:

 theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

之后的示例

 theAnimation.speed = 1.0f;   

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