Josh Bernfeld
Josh Bernfeld

Reputation: 4426

CABasicAnimation maintain a constant speed

I am trying to create a simple animation with CABasicAnimation that will always hold a constant speed, even if the distance the animation must travel is never the same. Here is my code so far, it is scrolling a label that will always vary in size, but cannot yet hold a constant speed. Help is much appreciated.

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"];

Upvotes: 1

Views: 1366

Answers (1)

frankWhite
frankWhite

Reputation: 1542

Did you try add:

 theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

in example after

 theAnimation.speed = 1.0f;   

?

This function set linear sped of animation.

Upvotes: 1

Related Questions