0

I'm trying to animate a change in the opacity of a CALayer using a CATransaction, but it isn't working:

[CATransaction begin];
[CATransaction setAnimationDuration:3];
[CATransaction setCompletionBlock:^{
    NSLog(@"ENTERING COMPLETION BLOCK");
}];
NSLog(@"BEGINNING TRANSACTION");
self.opacity = 1;
[CATransaction commit];

The opacity, which was 0 coming into this code, doesn't animate. It changes immediately, although the transaction does take 3 seconds before the completion block displays the message, "ENTERING COMPLETION BLOCK," as expected. I understand that this type of animation won't work on the main layer of a view, but that's not the case here.

In case you're wondering, I'm using a CATransaction rather than a CABasicAnimation because (1) I find the code a bit easier to read (and write), and (2) its use of completion blocks makes chaining easier to do (tho I'm not doing that here).

Is there some reason I'm not aware of that makes CATransaction a bad candidate for this type of animation?

1 Answer 1

0

It appears that the layers - (id<CAAction>)actionForKey:(NSString *)key no longer returns default animations for its animatable properties

You can fix this by subclassing a CALayer and doing overriding that method. Something like this:

- (id<CAAction>)actionForKey:(NSString *)key {
    if ([key isEqualToString:@"opacity"]) {
       return [CABasicAnimation animationWithKeyPath:key]
    }
   return nil
 }

Or if you can use the layers delegate method: - (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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