Re: CALayer Transitions
Re: CALayer Transitions
- Subject: Re: CALayer Transitions
- From: Gordon Apple <email@hidden>
- Date: Sun, 15 Nov 2009 22:38:04 -0600
- Thread-topic: CALayer Transitions
I had actually tried using that delegate method. It gets called a lot
because the same controller is delegate to many layers (for drawing). Of
course, I filter it for the my "content" layer. As you noted, using
"sublayers" also triggers for other sublayers and I may have to use your
flag method to prevent this.
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.
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
_______________________________________________
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