调整图层及其子图层的大小 - CoreAnimation

时间:2010-08-20 07:43:43

标签: iphone cocoa cocoa-touch macos core-animation

我有一个带有一个子层B的CALayer A. 我希望A调整大小(缩小),所以我将动画添加到我的A层 但是当我提交动画子层B时没有缩小。它的大小仍然存在(但它的位置随着超级层边界的变化而变化) 如何使我的B图层与A动画一起调整大小?

这就是我写的:

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

提交:

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

答案:

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

1 个答案:

答案 0 :(得分:7)

实现此目的的最简单方法是为keyPath“transform.scale”设置动画,而不是“bounds.size”。

如果您坚持使用“bounds.size”,则需要在A的委托上的A层或layoutSublayers上实施layoutSublayersOfLayer:(CALayer*)aLayer,并在该实施中手动调整B的大小。