52

I'm trying to execute a completion-block after my CAAnimation has finished. However, it seems that animation block is called before my animation completes. The animation still happens correctly though.

[CATransaction begin];
[self.view.layer addAnimation:self.dropAndBounceAnimation forKey:@"appearance"];
[CATransaction setCompletionBlock:completionBlock];
[CATransaction commit];

The dropAndBounceAnimation is a CAKeyFrameAnimation on position.y, with a fixed duration.

4 Answers 4

116

I'm not sure if this really is the correct fix, but by setting the completion-block before adding the animation for the layer, the completion-block is consistently called at the correct time.

[CATransaction begin];
[CATransaction setCompletionBlock:completionBlock];
[self.view.layer addAnimation:self.dropAndBounceAnimation forKey:@"appearance"];
[CATransaction commit];
4
  • 21
    Yes it is, as per the documentation: "The completion block object that is guaranteed to be called (on the main thread) as soon as all animations subsequently added by this transaction group have completed (or have been removed.)". The key part is "subsequently added".
    – albertamg
    Nov 14, 2013 at 11:10
  • 3
    @albertamg However, it also states If no animations are added before the current transaction group is committed (or the completion block is set to a different value,) the block will be invoked immediately.
    – iwasrobbed
    Feb 14, 2014 at 14:42
  • 1
    @iWasRobbed yes, and that explains why the block is called before the animation completes if it is set after adding the animation to the transaction group.
    – albertamg
    Feb 17, 2014 at 10:41
  • also called immediately if you forgot to set view.wantsLayer = YES;
    – codrut
    Oct 27, 2017 at 12:11
16

You need to set the completion block before adding the animation.

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat: 1.0f] forKey:kCATransactionAnimationDuration];

[CATransaction setCompletionBlock:^{
// ... whatever you want to do when the animation is complete
}];

[self.googleMapsView animateToCameraPosition:[GMSCameraPosition 
                    cameraWithLatitude:LATITUDE
                             longitude:LONGITUDE
                                  zoom:ZOOM]];

[CATransaction commit];

This must trigger the completion block after the completion of that animation on the view.

0
7

Here is Swift 3.0.1, Xcode 8 version:

CATransaction.begin()

CATransaction.setCompletionBlock({
  print("Transaction completed")
})

print("Transaction started")
view.layer.add(dropAndBounceAnimation, forKey: "appearance")

CATransaction.commit()
0

Try to start the animation asynchronously:

DispatchQueue.main.async {
    self.startAnimation()
}

because it can interfere with view drawing if you make some view setup before calling the animation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.