Re: CALayer Transitions
Re: CALayer Transitions
- Subject: Re: CALayer Transitions
- From: Matt Neuburg <email@hidden>
- Date: Mon, 16 Nov 2009 09:23:50 -0800
- Thread-topic: CALayer Transitions
On Sun, 15 Nov 2009 22:38:04 -0600, Gordon Apple <email@hidden> said:
> What I don't understand, is that, according to the core animation guide,
>kCATransition seems to be what I want for a key instead of "sublayers". The
>guide says this is triggered by "replaceSublayer: with:". However, stepping
>it through, I never see this key come through the delegate.
You seem to be imagining that you can call addAnimation:forKey: and then the
animation will just lurk there, waiting for something called the "key" to
come along, and then the animation will trigger. That is not what
addAnimation:forKey: does, and it isn't what the "key" means.
[Hmmm... The cause of your confusion might be that a view has something
called defaultAnimationForKey:. It has an animations dictionary, so you can
install an animation in that dictionary, under a key, and that key will be
used to hunt to for an animation when a property is changed. But a layer is
not a view; so that has nothing to do with what we're discussing now.]
As I said before, to see what addAnimation:forKey: does and what the key is,
look at the Animation section of the Core Animation Programming Guide. The
animation is performed right now (and in the case of a BasicAnimation,
*what* it animates is a property, designated by its keypath). The "key" is
just an arbitrary name to help you refer to this animation later, if needed
(as in removeAnimation:forKey:).
m.
>On 11/15/09 8:51 PM, "Matt Neuburg" <email@hidden> wrote:
>
>> On Sat, 14 Nov 2009 14:58:31 -0600, Gordon Apple <email@hidden> said:
>>> I assume this should be simple, but so far I haven't found the magic
>>> incantation, even after reading the docs, Dudley's book, and some archives.
>>>
>>> Problem: Layer called "contentLayer" has sublayers containing layer A,
>>> which is to be transitioned to layer B. (Note: Using GC here.) Controller
>>> code:
>>>
>>> -(void)transitionFrom:(CALayer*)A to:(CALayer*)B{
>>>
>>> CATransition* trans = [CATransition animation];
>>> trans.type = kCATransitionPush;
>>> trans.subtype = kCATransitionFromLeft;
>>> trans.speed = 0.5;
>>>
>>> [[self contentLayer] addAnimation:trans forKey:kCATransition];
>>> [[self contentLayer] replaceSublayer:A with:B];
>>> }
>>>
>>> My understanding (likely wrong) is that replaceSublayer triggers the
>>> necessary action to start the transition, then the animation object is
>>> automatically removed (default).
>>>
>>> I get an initial transition, then on subsequent calls get layer
>>> replacement but no transition. What am I missing?
>>
>> Your understanding is likely wrong. :) addAnimation:forKey: on a layer
>> triggers the animation then and there. Look at the examples in the Animation
>> section of the Core Animation Programming Guide.
>>
>> That, however, isn't what you want. You want the action of replacing
>> sublayer A with sublayer B to trigger the animation. For that, look at the
>> Actions section of the Core Animation Programming Guide.
>>
>> For my money, the easiest way to set this up is to make self your
>> contentLayer's delegate and implement actionForLayer:forKey:. So, assuming
>> the delegation is already set up:
>>
>> -(void)transitionFrom:(CALayer*)A to:(CALayer*)B {
>> [[window.contentView layer] replaceSublayer:A with:B];
>> }
>>
>> - (id<CAAction>)actionForLayer:(CALayer *)theLayer
>> forKey:(NSString *)theKey {
>> if ([theKey isEqualToString:@"sublayers"]) {
>> CATransition* trans = [CATransition animation];
>> trans.type = kCATransitionPush;
>> trans.subtype = kCATransitionFromLeft;
>> trans.speed = 0.5;
>> return trans;
>> }
>> return [NSNull null]; // or whatever
>> }
>>
>> To be cleverer about when to trigger the animation and when not, just raise
>> an ivar flag in self. For example, you might like to animate when certain
>> sublayers are added but not others. So transitionFrom would raise a flag and
>> actionForLayer could check that flag (in addition to checking the key).
>> That's why I like this approach: it's so flexible. However, there are other
>> ways to do it.
>>
>> By the way, there is an example almost *exactly* like this in the Actions
>> section of the Core Animation Programming Guide. m.
>
>--
>Gordon Apple
>Ed4U
>Little Rock, AR
>
>
>
>
>
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden