views:

816

answers:

2

Hello all -

How can I use two separate CABasicAnimations to simultaneously animate the same property?

For example Two CABasicAnimations, both animate position.y.

The first animation will be a bounce (from 100, to 300, duration = 1, autoreverse = yes, repeatcount = 10)

The second animation will be a slow scroll (by 100, duration = 10)

The behavior I am seeing is that if the first animation is in progress & I use:

[pickle.layer addAnimation:[self makescrollanimation] forKey:@"scrollit"];

to add the second... the second is ignored.

I know the second animation works, because if i start with the second one, then the first is ignored.

Thank you- Matt

A: 

You can use CAAnimationGroup if you want to bundle multiple animations across different properties, but I'm not sure this is possible using two CABasicAnimations. You can only apply one property animation per property to a view at a time.

One way I could think of accomplishing this would be to nest your view in another UIView, and perform the 'scrollit' animation on the enclosing view, while continuing to bounce the enclosed view.

Love to know a better answer!

GoldenBoy
+3  A: 

If I understand you correctly, you want the view to bounce up and down while the whole bouncing motion shifts slowly downwards. You can do this by making the animations additive using their additive property. For example:

CABasicAnimation *bounceAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounceAnimation.duration = 1;
bounceAnimation.fromValue = [NSNumber numberWithInt:100];
bounceAnimation.toValue = [NSNumber numberWithInt:300];
bounceAnimation.repeatCount = 10;
bounceAnimation.autoreverses = YES;
bounceAnimation.fillMode = kCAFillModeForwards;
bounceAnimation.removedOnCompletion = NO;
bounceAnimation.additive = YES;
[view.layer addAnimation:bounceAnimation forKey:@"bounceAnimation"];

CABasicAnimation *scrollAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
scrollAnimation.duration = 10;
scrollAnimation.fromValue = [NSNumber numberWithInt:0];
scrollAnimation.toValue = [NSNumber numberWithInt:100];
scrollAnimation.fillMode = kCAFillModeForwards;
scrollAnimation.removedOnCompletion = NO;
scrollAnimation.additive = YES;
[view.layer addAnimation:scrollAnimation forKey:@"scrollAnimation"];

should accomplish the animation you desire, if I'm reading your question correctly. The scroll animation should be able to be triggered at any point during the bounce animation.

Brad Larson
Yes, it seems that additive / with a fromValue of 0 means to move from the layer's current position to the current position + toValue.I had been trying to use "byValue" -- but when "byValue" is combined with "additive" the results don't make sense.Thank you!Matt