RSS

Cocoa Code Snippet: Simple Window Fade In/Out

Continuing the Cocoa Code Snippet series, I want to put up a nice useful method I add to most of my NSWindows. Consider making it a category. This bit of code will allow your windows to fade in and out nicely when they are closed. Before Leopard and Core Animation, you had to write your own timer’s to get this effect. Now all you have to do is call this method:

1
2
3
4
5
6
7
8
9
10
11
12
13
-(IBAction)displayOrCloseWindow:(id)sender {
	//Fades in & out nicely
	if(isOpen) {
		[[self animator] setAlphaValue:0.0];
		isOpen = NO;
	}
	else {
		[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
		[self makeKeyAndOrderFront:self];
		[[self animator] setAlphaValue:1.0];
		isOpen = YES;
	}
}

You need to keep a instance variable: BOOL isOpen to let us know if the window is open or not. This method then just checks that value and either animates to 0 alpha or activates itself and animates to alpha 1.0. This is a really simple code snippet, but I find it extremely useful in almost every project.

  • Marceau... your ip gives you away.
  • guess
    bout time you did somethin noob
blog comments powered by Disqus
« Instantiating an Object in Interface Builder on Leopard | Last.fm’s New Feature + WireTap = Illegal (Maybe?) »