Search code examples
ioscore-animationcalayercgpathios-animations

CAShapeLayer, animating path with Transactions


I wondering why when I try to animate the path property of a CAShapeLayerwith a basic animation it works but when I try to do it with transaction it doesn't.

I've successfully animated other animatable properties using just transaction. Here is my current code:

    CATransaction.begin()
    CATransaction.setAnimationDuration(2.0)
        path = scalePath() // a scaled version of the original path
    CATransaction.commit()

the new path is obtained scaling the original path with this (very hardcoded) function inside an extension of CAShapeLayer:

func scalePath()->CGPath{
    var scaleTransform =  CGAffineTransform.identity.translatedBy(x: -150, y: -150)
    scaleTransform = scaleTransform.scaledBy(x: 10, y: 10)
    let newPath = path?.copy(using: &scaleTransform)

    return newPath!
}

Can you identify any issue?


Solution

  • The answer is simple but a bit unsatisfactory: while the path property is animatable, it doesn't support implicit animations. This is called out in the Discussion section of the documentation for the path property:

    Unlike most animatable properties, path (as with all CGPathRef animatable properties) does not support implicit animation.

    Explicit vs. Implicit animations

    An "explicit" animation is an animation object (e.g. CABasicAnimation) that is explicitly added to the layer by calling -addAnimation:forKey: on the layer.

    An "implicit" animation is an animation that happens implicitly as a result of changing an animatable property.

    The animation is considered implicit even if the property is changed within a transaction.