Re: CALayer Transitions
Re: CALayer Transitions
- Subject: Re: CALayer Transitions
- From: Matt Neuburg <email@hidden>
- Date: Sun, 15 Nov 2009 18:51:46 -0800
- Thread-topic: CALayer Transitions
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.
--
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