【问题标题】:Change height of layer using caanimation使用 caanimation 更改图层的高度
【发布时间】:2015-02-21 10:44:25
【问题描述】:

我正在学习动画,但我遇到了一个问题。 我想显示一个高度从 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高度和红色笔划的矩形)从底部移动到中间......高度没有增加。

【问题讨论】:

标签: ios objective-c calayer caanimation


【解决方案1】:

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

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

【讨论】:

  • 感谢您的解释:)
猜你喜欢
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 2017-02-02
  • 2018-03-02
相关资源
最近更新 更多