使用 caanimation 改变层的高度

Change height of layer using caanimation

我正在学习 caanimations,我遇到了一个问题。 我想显示一个高度从 1 开始到 100 的矩形。我希望这个矩形保持在视图的中间,只是扩展它的高度。

这是我到目前为止编写的代码:

CAShapeLayer *rect = [CAShapeLayer layer];
CGRect frame = CGRectMake(0, 0,destView.frame.size.width, 1);

rect.frame = frame;
rect.path = [UIBezierPath bezierPathWithRect:frame].CGPath;
//DestView is the view that will receive this layer
rect.position = destView.center;
//For debug purpose
rect.fillColor = [UIColor blackColor].CGColor;
rect.strokeColor = [UIColor redColor].CGColor;
rect.anchorPoint = CGPointMake(0.5, 0.5);

CABasicAnimation *test = [CABasicAnimation animationWithKeyPath:@"bounds.size.height"];
[test setFromValue:[NSNumber numberWithFloat:1.0f]];
[test setToValue:[NSNumber numberWithFloat:100.0f]];
test.duration = 5.0f;
[test setFillMode:kCAFillModeForwards];

[rect addAnimation:test forKey:@"rectGrowing"];
[destView.layer addSublayer:rect];

我得到的结果是红线(高度为 1 的矩形和红色描边)从底部移动到中间...高度没有增加。

根据您的代码,高度应该会增加。 但是,您应该使用 backgroundColor 而不是 fillColor。

这是因为虽然高度变了,但是路径没有变。