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!
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.