Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate CALayer hide

I'm trying to hide a CALayer after a few microseconds and I'm using CABasicAnimation to animate the hide.

At the moment I'm trying to use

[aLayer setHidden:YES];

CABasicAnimation * hideAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
[hideAnimation setDuration:aDuration];
[hideAnimation setFromValue:[NSNumber numberWithBool:NO]];
[hideAnimation setToValue:[NSNumber numberWithBool:YES]];
[hideAnimation setBeginTime:0.09];
[hideAnimation setRemovedOnCompletion:NO];
[hideAnimation setDelegate:self];

[alayer addAnimation:hideAnimation forKey:@"hide"];

But when I run this, the layer is hidden immediately, rather than waiting for the desired beginTime.

I'm uncertain about my keyPath as "hidden" but couldn't find any other option and the documentation does state that the hidden property of a CALayer is animatable.

What's the correct way to achieve what I'm looking for?

like image 339
Tom Irving Avatar asked Jul 15 '10 22:07

Tom Irving


3 Answers

Try animating the opacity property instead. Go from 1.0 to 0.0 and you should get the effect you want.

like image 130
Alex Avatar answered Nov 18 '22 07:11

Alex


From CAMediaTiming.h, it says about beginTime property:

The begin time of the object, in relation to its parent object, if applicable. Defaults to 0.

You should use CACurrentMediaTime() + desired time offset.

[hideAnimation setBeginTime:CACurrentMediaTime() + 0.09];
like image 43
Miroslav Kovac Avatar answered Nov 18 '22 09:11

Miroslav Kovac


I'm sure this is too late to do the original poster any good, but it may help others. I've been trying to do something similar, except to make the animation implicit when the hidden property is changed. As Tom says, animating opacity doesn't work in that case, as the change to the layer's hidden property seems to take effect right away (even if I delay the animation with beginTime).

The standard implicit action uses a fade transition (CATransition, type = kCATransitionFade), but this operates on the whole layer and I want to perform another animation at the same time, which is not a compatible operation.

After much experimentation, I finally noticed @Kevin's comment above and --- hello! --- that actually works! So I just wanted to call it out so the solution is more visible to future searchers:

CAKeyframeAnimation* hiddenAnim = [CAKeyframeAnimation animationWithKeyPath:@"hidden"];
hiddenAnim.values = @[@(NO),@(YES)];
hiddenAnim.keyTimes = @[@0.0, @1.0];
hiddenAnim.calculationMode = kCAAnimationDiscrete;
hiddenAnim.duration = duration;

This delays the hiding until the end of the duration. Combine it with other property animations in a group to have their effects seen before the layer disappears. (You can combine this with an opacity animation to have the layer fade out, while performing another animation.)

Thank you, Kevin!

like image 2
big_m Avatar answered Nov 18 '22 07:11

big_m