CATransaction completion being called immediately

IosCaanimation

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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];

Solution 2 - Ios

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.

Solution 3 - Ios

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()

Solution 4 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJavacheView Question on Stackoverflow
Solution 1 - IosJavacheView Answer on Stackoverflow
Solution 2 - IosSaruView Answer on Stackoverflow
Solution 3 - IosVadim BulavinView Answer on Stackoverflow
Solution 4 - IosmixelView Answer on Stackoverflow