CABasicAnimation扩展高度方向

时间:2012-08-30 09:47:12

标签: iphone objective-c ios core-animation cabasicanimation

我有一个包含CALayer的UIView(“layerPlot_”) 当y调用以下方法时它可以工作,但它会在两个方向(向上和向下)扩展高度我想指定是否要向下或向上扩展高度,并且图层的另一边保持在同一位置。

代码如下:

/**
 * Resizes the layer to a new height.
 *
 * @param newHeight the new height of the layer
 */
-(void)resizeLayerTo:(CGFloat)newHeight {

// Create animation
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size.height"];

[animation setFromValue:[NSNumber numberWithFloat:0.0f]];
[animation setToValue:[NSNumber numberWithFloat:newHeight]];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[animation setFillMode:kCAFillModeForwards];
[animation setDuration:1.2f];

// Update the layer's bounds so the layer doesn't snap back when the animation completes.
CGRect newSize = [layerPlot_ bounds];
newSize.size.height = newHeight;
layerPlot_.bounds = newSize;

// Add the animation, overriding the implicit animation.
[layerPlot_ addAnimation:animation forKey:@"bounds"];
}

感谢。

1 个答案:

答案 0 :(得分:1)

您可以自己进行计算并为框架设置动画而不是边界,也可以将图层的anchorPoint更改为底部,然后更新边界。

请注意,它将相对于该点绘制所有内容,因此必须更新位置,否则它将向上移动一半宽度。