Joas Jern
Joas Jern

Reputation: 23

CABasicAnimation Continue from fromValue

How can i continue a UIImageView transform.translation.x animation after it completed a toValue Process.for example:when press the button#1:from 1 to 55,press button#2 from 55 to 110....and if it at the position of button#2,and you click button#5,then from 55*2 to 55*5.

- (void)animateArrow{
CABasicAnimation *theAnimation;


theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
theAnimation.duration=0.4;
theAnimation.repeatCount=1;
theAnimation.toValue=[NSNumber numberWithFloat:50];

[buttonArrow.layer setValue:theAnimation.toValue forKey:theAnimation.keyPath];
[buttonArrow.layer addAnimation:theAnimation forKey:@"transform.translation.x"];

}

Upvotes: 0

Views: 2206

Answers (1)

Anil
Anil

Reputation: 49

You should think about providing multiple points along the animation path in an array like this:

CAKeyframeAnimation *downMoveAnimation;
downMoveAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
downMoveAnimation.duration = 12;
downMoveAnimation.repeatCount = 1;
downMoveAnimation.values = [NSArray arrayWithObjects:           
                             [NSNumber numberWithFloat:20], 
                             [NSNumber numberWithFloat:220], 
                             [NSNumber numberWithFloat:290], nil]; 
downMoveAnimation.keyTimes = [NSArray arrayWithObjects:     
                               [NSNumber numberWithFloat:0], 
                               [NSNumber numberWithFloat:0.5], 
                               [NSNumber numberWithFloat:1.0], nil]; 

downMoveAnimation.timingFunctions = [NSArray arrayWithObjects:
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],        // from keyframe 1 to keyframe 2
                                      [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], nil]; // from keyframe 2 to keyframe 3

downMoveAnimation.removedOnCompletion = NO;
downMoveAnimation.fillMode = kCAFillModeForwards;

Hope this works.

The main difference between basic animation and key-frame animation is that Key-frame allows you to specify multiple points along the path.

Upvotes: 1

Related Questions