views:

274

answers:

1

Hi there.

In the past I've been successfully able to fade in an NSWindow using the following code;

if (![statusWindow isVisible])
    {
statusWindow.alphaValue = 0.0;
[statusWindow.animator setAlphaValue:1.0];
    }

CAAnimation *anim = [CABasicAnimation animation];
[anim setDelegate:self];
[statusWindow setAnimations:[NSDictionary dictionaryWithObject:anim forKey:@"alphaValue"]];
[statusWindow makeKeyAndOrderFront:self];

For my current project I'm trying to make a flash similar to the one in Photo Booth. I've created a white NSPanel and was planning to set my NSWindow's content to the panel, and quickly set it back.

Is it possible to set the contentView of an NSWindow using a nice fade effect?

P.S - If there is an easier way you know of how to achieve the flash, please tell me!

Thanks in advance, Ricky.

+1  A: 

Why use another window? It looks like you're trying to use CoreAnimation already so why not just add a white CALayer to your existing view and animate its opacity?

Matt Lilek
I thought about that, but I can't find a way to add a layer to an NSWindow?
Ricky
Assuming that your entire window is using CA then you can get the windows content view with [ myWindow contentView ] and then add your status layer to that. If you set the autoresizingMask appropriately you can make it automatically stay the same size as the window as well.
Jon Steinmetz
"but I can't find a way to add a layer to an NSWindow"You're thinking about it wrong.Think of NSWindow's as containers for your content view and and other windows and NSView's as containers for other views and layers.You never add a layer directly to a window, only to a subview of that window (be it -contentView or one of your own views) and very rarely will you actually be swapping the -contentView of an NSWindow. A house goes on top of a foundation. Sometimes you'll have to fix a crack or two, but you won't replace it after the house is built.
Matt Lilek
Okay. I tried: [[mainWindow contentView] addSublayer:whiteLayer]; That returns the error in the console: -[NSView addSublayer:]: unrecognized selector sent to instance 0x100265340Am I going about it the right way?Ricky.
Ricky
No. What does [mainWindow contentView] return? An NSView. On what class is -addSublayer: defined on? On CALayer. Hopefully you should see now, that you need to get a CALayer from that NSView so: [[[mainWindow contentView] layer] addSublayer:whiteLayer];
Matt Lilek
Thanks Matt. Except I must be doing something wrong with my CALayer here, because nothing is displaying on the window; CALayer *whiteFlasher = [CALayer layer]; whiteFlasher.backgroundColor = CGColorGetConstantColor(kCGColorWhite); whiteFlasher.position = CGPointMake(50, 50); whiteFlasher.bounds = CGRectMake (0, 0, 1920, 1000); [[[windowContent contentView] layer] addSublayer:whiteFlasher];
Ricky