CATransaction completion being called immediately

Clash Royale CLAN TAG#URR8PPP
CATransaction completion being called immediately
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
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];
@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 '14 at 14:42
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 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 '14 at 10:41
also called immediately if you forgot to set
view.wantsLayer = YES;– codrut
Oct 27 '17 at 12:11
view.wantsLayer = YES;
You need to set the animation 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.
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()
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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 '13 at 11:10