CAShapeLayer 路径动画

Posted

技术标签:

【中文标题】CAShapeLayer 路径动画【英文标题】:CAShapeLayer Path Animation 【发布时间】:2017-03-22 12:59:47 【问题描述】:

我正在尝试为特定形状的路径更改设置动画。我可以通过用简单的 .path [cgpath] 设置器替换此代码来手动更改而无需动画。但是,我想为美学的变化制作动画。

执行此代码时,尽管日志显示动画已完成,但形状并没有发生任何变化。

for (CALayer *layer in self.view.layer.sublayers) 
    if ([layer isKindOfClass:[CAShapeLayer class]]) 
        CAShapeLayer *copy = (CAShapeLayer *)layer;
        if ([sideChoice isEqualToString:@"datum"]) 
            if ([copy.name isEqualToString:@"datumSideLayer"]) 
                [CATransaction begin];
                [CATransaction setCompletionBlock:^
                    NSLog(@"Animation complete.");
                ];
                CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
                morph.duration = 1;
                morph.toValue = pathChange;
                [copy addAnimation:morph forKey:nil];

                [CATransaction commit];
                break;
            
         else if ([sideChoice isEqualToString:@"opposite"]) 
            if ([copy.name isEqualToString:@"oppositeSideLayer"]) 

            
        
    

【问题讨论】:

【参考方案1】:
for (CALayer *layer in self.view.layer.sublayers) 
    if ([layer isKindOfClass:[CAShapeLayer class]]) 
        CAShapeLayer *copy = (CAShapeLayer *)layer;
        if ([sideChoice isEqualToString:@"datum"]) 
            if ([copy.name isEqualToString:@"datumSideLayer"]) 
                [CATransaction begin];
                [CATransaction setCompletionBlock:^
                    NSLog(@"Animation complete.");
                ];
                CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"];
                morph.duration = 1;
                morph.fromValue = (id) copy.path;
                morph.toValue = (id) pathChange.CGPath;
                [copy addAnimation:morph forKey:nil];

                copy.path = [pathChange CGPath];
                [CATransaction commit];
                break;
            
         else if ([sideChoice isEqualToString:@"opposite"]) 
            if ([copy.name isEqualToString:@"oppositeSideLayer"]) 

            
        
    

我没有正确设置 fromValue 也没有指向 CGPath。

【讨论】:

以上是关于CAShapeLayer 路径动画的主要内容,如果未能解决你的问题,请参考以下文章

CAShapeLayer 路径动画

如何为 CAShapeLayer 路径和填充颜色设置动画

CAShapeLayer 动画路径毛刺/闪烁(从椭圆到矩形并返回)

我正在为填充但未描边的 CAShapeLayer 的路径值设置动画,我想让它的动画更流畅

贝塞尔曲线与CAShapeLayer的关系以及Stroke动画

SpriteKit - 动画 SKShapeNode 形状路径