我正在学习 caanimations,我遇到了一个问题。 我想显示一个高度从 1 开始到 100 的矩形。我希望这个矩形保持在 View 的中间,只是扩大它的高度。
这是我到目前为止所做的代码:
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。
这是因为虽然高度改变了,但路径却没有。
关于ios - 使用 caanimation 改变层的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28644832/