I would like to effect the scale and translation of a layer's transform at the same time but only the translation animation runs. Here is the errant code...
Here I create the scale animation with the keypath to scale.y:
self.mainImage.layer.anchorPoint = CGPointMake(0.5, 1);
CAKeyframeAnimation *mainAnimationScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
mainAnimationScale.duration = 0.75;
mainAnimationScale.repeatCount = 0;
mainAnimationScale.removedOnCompletion = YES;
mainAnimationScale.autoreverses = NO;
mainAnimationScale.fillMode = kCAFillModeForwards;
mainAnimationScale.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.7],
[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.85],
[NSNumber numberWithFloat:1.0],
nil];
mainAnimationScale.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.65],
[NSNumber numberWithFloat:0.72],
[NSNumber numberWithFloat:0.84],
[NSNumber numberWithFloat:0.91],
[NSNumber numberWithFloat:1],
nil];
Here I create the translation animation with the keypath to translation.y:
CAKeyframeAnimation *mainAnimationTrans = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
mainAnimationTrans.duration = 0.75;
mainAnimationTrans.repeatCount = 0;
mainAnimationTrans.removedOnCompletion = YES;
mainAnimationTrans.autoreverses = NO;
mainAnimationTrans.fillMode = kCAFillModeForwards;
mainAnimationTrans.values = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:-250.0],
[NSNumber numberWithFloat:-250.0],
[NSNumber numberWithFloat:0],
nil];
mainAnimationTrans.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0],
[NSNumber numberWithFloat:0.65],
[NSNumber numberWithFloat:1],
nil];
Here I group them together and apply them to the UIView's layer:
CAAnimationGroup *theGroup = [CAAnimationGroup animation];
theGroup.duration = 0.75;
theGroup.repeatCount = 0;
theGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
theGroup.animations = [NSArray arrayWithObjects:mainAnimationTrans, mainAnimationScale, nil];
[self.mainImage.layer addAnimation:theGroup forKey:@"layerAnimation"];
I initially tried adding the animations directly to the layer like this, but that didn't work either:
[self.mainImage.layer addAnimation:mainAnimationTrans forKey:@"transform.scale.y"];
[self.mainImage.layer addAnimation:mainAnimationTrans forKey:@"transform.translation.y"];
I've tried every string I can think for for the keyPath values and I'm guessing it has something to do with the fact that I'm animating two aspects of the transform, but I thought that using the group would fix that. Alas...
The key to making this work is combining transforms. I don't know why I didn't realize this sooner since it is basic Matrix math...I think my co-worker even mentioned it yesterday. Ack.
At any rate, the animation I was going for was to have an item drop down and bounce once it reached its destination. Here is the code:
Things to note:
Hope this helps!