简体   繁体   English

动画CAShapeLayer

[英]Animation CAShapeLayer

I draw a graph with this code: 我用这段代码画了一个图:

CAShapeLayer *curentGraph = [CAShapeLayer new];
CGMutablePathRef linePath = CGPathCreateMutable();
curentGraph.lineWidth = 3.0f;
curentGraph.fillColor = [[UIColor clearColor] CGColor];
curentGraph.strokeColor = [colorGraph CGColor];
for (NSValue *value in arrOfPoints) {
    CGPoint pt = [value CGPointValue];
    CGPathAddLineToPoint(linePath, NULL, pt.x,pt.y);
};
curentGraph.path = linePath;CGPathRelease(linePath);
[self.layer addSublayer:curentGraph];

and it looks like this 它看起来像这样

图表截图

But I have a problem. 但我有一个问题。 I need to animate the graph as it appears. 我需要为图形显示动画。 Every point should move up from position y = 0 to y = pt.y . 每个点应该从位置y = 0向上移动到y = pt.y Like they do in the graph on this site . 就像他们在本网站的图表中所做的那样。

How do I animate my graph like that? 如何为我的图形设置动画?

The path property on CAShapeLayer is animatable. CAShapeLayer上的path属性是可动画的。 This means that you can create one path where every y value is 0.0 and the animate from that path to the real graph. 这意味着您可以创建一个路径,其中每个y值为0.0并且从该路径到实际图形的动画。 Just make sure that the paths have the same number of points. 只需确保路径具有相同的点数。 This should be easy, since you already have the loop. 这应该很简单,因为你已经有了循环。

CGMutablePathRef startPath = CGPathCreateMutable();
for (NSValue *value in arrOfPoints) {
    CGPoint pt = [value CGPointValue];
    CGPathAddLineToPoint(startPath, NULL, pt.x, 0.0);
}

Then you can animate the path by creation a CABasicAnimation for the @"path" key. 然后,您可以通过为@"path"键创建CABasicAnimation来为路径设置动画。

CABasicAnimation *pathAppear = [CABasicAnimation animationWithKeyPath:@"path"];
pathAppear.duration = 2.0; // 2 seconds
pathAppear.fromValue = (__bridge id)startPath;
pathAppear.toValue   = (__bridge id)linePath;

[yourShapeLayer addAnimation:pathAppear forKey:@"make the path appear"];

Here is a CAShapeLayer subclass that'll allow you to animate its path implicitly (without having to declare a CABasicAnimation ): 这是一个CAShapeLayer子类,它允许您隐式地为其path设置动画(无需声明CABasicAnimation ):

Interface: 接口:

@interface CAShapeLayerAnim : CAShapeLayer
@end

Implementation: 执行:

@implementation CAShapeLayerAnim

- (id<CAAction>)actionForKey:(NSString *)event {
    if ([event isEqualToString:@"path"]) {
        CABasicAnimation *animation = [CABasicAnimation
            animationWithKeyPath:event];
        animation.duration = [CATransaction animationDuration];
        animation.timingFunction = [CATransaction
            animationTimingFunction];
        return animation;
    }
   return [super actionForKey:event];
}

@end

For animation you need use strokeStart and strokeEnd properties of CAShapeLayer. 对于动画,您需要使用strokeStartstrokeEnd属性。

See example CAShapeLayer animation in Ole Begemann blog post . 请参阅Ole Begemann博客文章中的示例CAShapeLayer动画。

From documentation strokeStart : 从文档strokeStart

The value of this property must be in the range 0.0 to 1.0. 此属性的值必须在0.0到1.0的范围内。 The default value of this property is 1.0. 此属性的默认值为1.0。

Combined with the strokeEnd property, this property defines the subregion of the path to stroke. 结合strokeEnd属性,此属性定义笔划路径的子区域。 The value in this property indicates the relative point along the path at which to begin stroking while the strokeEnd property defines the end point. 此属性中的值表示沿strokeEnd属性定义结束点时开始描边的路径的相对点。 A value of 0.0 represents the beginning of the path while a value of 1.0 represents the end of the path. 值0.0表示路径的开头,而值1.0表示路径的结尾。 Values in between are interpreted linearly along the path length. 其间的值沿路径长度线性解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM