Search code examples
iosswiftcore-animationcashapelayer

Animating CAShapeLayer: Do On Completion


I have a CAShapeLayer, whose strokeEnd I occasionally update inside of an NSTimer loop. Works great, when I call:

myPathLayer.strokeEnd += 0.1

this path change automatically animates as I want it to. My question is, as this isn't a function call, there isn't a completion block. I'd like to perform another action once this animation completes. How can I achieve this?


Solution

  • You can wrap in with a CATransaction to set a completion block for layer animations.

    CATransaction.begin()
    CATransaction.setCompletionBlock({self.myFunction()})
    myPathLayer.strokeEnd += 0.1
    CATransaction.commit()
    

    Hopefully there is a better way, but this is how I've done it.