CAShapeLayer Path Animation

710 Views Asked by At

I'm trying to animate a path change on a particular shape. I can manually change without animation by replacing this code with a simple .path [cgpath] setter. However, I want to animate the change for aesthetics.

When this code executes, there is no change to the shape what-so-ever although the log suggests the animation is complete.

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

There are 1 best solutions below

0
Daniel On
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"]) {

            }
        }
    }
}

I didn't correctly set a fromValue nor was I pointing to a CGPath.