ios - 使用 caanimation 改变层的高度

标签 ios objective-c calayer caanimation

我正在学习 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/

相关文章:

ios - 手机处于静音模式时,react-native-youtube没有视频声音

ios - 将 PFFile 转换为 UIImage

ios - 没有 IB 的 Xcode UIViewController 到 customUIViewController

objective-c - 复制 Realm 对象而不添加到数据库

ios - 在 AFNetworking 的 JSONRequestOperationWithRequest :success:failure: method, 中,JSON 响应是什么类型的对象?

iphone - 如何将我的应用链接到 App Store 开发者页面?

iphone - 在我的DrawRect中添加多个CALayers作为子图层时,应用程序崩溃

objective-c - 带子层的 CALayer hitTest

android - 手机间隙 : local notification repeat every Sunday of the week?

ios - 在一个操作中禁用所有 CALayer 默认操作