Temporary Core Animation

Working on my project I used Core Animation for the first time. Unfortunately layer-backed views don’t work well with some Cocoa controls (such as NSTextField and NSButton with check style). Luckyly I only need Core Animation to set up some NSView replacing and NSWindow scaling transitions.


For this reason I started to research a way to turn on layer backing before the animation starts and turn it off as soon as it ends. Since I didn’t find much information about it I wrote this post to sum it up. Here is some source code:

-(IBAction) trigger:(id) sender
{
	[[mainWindow contentView] setWantsLayer:YES];
	[NSTimer scheduledTimerWithTimeInterval:.2 target:self 
                       selector:@selector(doAnimation) userInfo:nil repeats:NO];

}
-(void)doAnimation
{
	[NSAnimationContext beginGrouping];
	// do your transition here
	[NSAnimationContext endGrouping];
	[NSTimer scheduledTimerWithTimeInterval:.1 target:self 
                       selector:@selector(finishAnimation) userInfo:nil repeats:NO];

}
-(void)finishAnimation
{
	[[mainWindow contentView] setWantsLayer:NO];
}

Have fun with Core Animation!

Advertisements

2 Comments »

  1. Alejandro Rodríguez said

    Hello friend,

    you use of timers seem to be quite an overkill. You could simply and much more efficiently use: performSelector:withObject:afterDelay:
    you would get the same result without the overhead of the NSTimer. Also all animations have a animationDidStop:finished: delegate method which you can use to know when your animations are done. Hope you can be useful to anyone else that stumbles with this post :). Cheers!

    • pigoz said

      Thank you, Alejandro! That will help me to polish some of my code.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: