Resizing Layers and Sublayers - CoreAnimation

I have CALayer A with one sublayer B. I want A to be resized (to contract), so I add animations to my layer A but when I fix the animation sublayer B, it does not contract. Its size remains (but its position changes as the boundaries of the super layer change) How can I change the size of layer B with the animation?

Here is what I wrote:

CABasicAnimation *fadeInAnimation; fadeInAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; fadeInAnimation.repeatCount = 1; fadeInAnimation.autoreverses = NO; fadeInAnimation.fromValue = [NSNumber numberWithFloat:1.0]; fadeInAnimation.toValue = [NSNumber numberWithFloat:0.0]; CABasicAnimation *shrinkAnimation; shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"]; shrinkAnimation.repeatCount = 1; shrinkAnimation.autoreverses = NO; shrinkAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; shrinkAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(0, 0)]; aniGroupOFF = [[CAAnimationGroup animation] retain]; aniGroupOFF.delegate = self; aniGroupOFF.duration = ANI_DURATION; aniGroupOFF.animations = [NSArray arrayWithObjects:shrinkAnimation, fadeInAnimation, nil]; 

And fixing:

 [self addAnimation:aniGroupOFF forKey:@"shrinkAndFade"]; self.opacity = 0.0; 

Answer:

 CABasicAnimation *shrinkAnimation; shrinkAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; //use transform instead of bounds.size shrinkAnimation.repeatCount = 1; shrinkAnimation.autoreverses = NO; shrinkAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; shrinkAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DScale(self.transform, 0.1, 0.1, 1.0)]; aniGroupOFF = [[CAAnimationGroup animation] retain]; aniGroupOFF.delegate = self; aniGroupOFF.duration = ANI_DURATION; aniGroupOFF.animations = [NSArray arrayWithObjects:shrinkAnimation, nil]; 
+4
source share
1 answer

The easiest way to achieve this is to animate keyPath "transform.scale" instead of "bounds.size".

If you stick to "bounds.size", you need to implement layoutSublayers at level A or layoutSublayersOfLayer:(CALayer*)aLayer in deletion and manually adjust the size B in this implementation.

+7
source

All Articles