Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade in/Fade out for MAAttachedWindow

I'm using Matt Gemmell's MAAttachedWindow (http://mattgemmell.com/source) with an NSStatusItem to display a custom view in the menu bar. I'm confused as to how to get it to fade in and fade out. Normally I'd do something like this:

[window makeKeyAndOrderFront:self];
[[window animator] setAlphaValue:1.0];

and to fade out:

[[window animator] setAlphaValue:0.0];

However this code seems to have no effect with MAAttachedWindow. Any ideas?

Thanks

like image 984
indragie Avatar asked Oct 10 '09 18:10

indragie


People also ask

What is a fade-in and fade-out used for?

The Fade In/Fade Out behavior is useful for introducing and removing animated elements. For example, you can apply the Fade In/Fade Out behavior to text that moves across the screen to make it fade into existence, and then fade away at the end of its duration.

What is a fade-in fade-out shot?

Fade In/Out A fade is when the scene gradually turns to a single color — usually black or white — or when a scene gradually appears on screen. Fade-ins occur at the beginning of a film or scene, while fade-outs are at the end.

What is the meaning of fading in fading out?

an optical effect in which a shot appears gradually out of darkness and then gradually disappears.


2 Answers

Sorry to drudge up an old post, but I thought it worthwhile mentioning that it works just fine for me to set the alpha value directly, with no need to add accessors/getters.

Simply doing (depending on your setup, or course):

[[self window] addChildWindow:attachedWindow ordered:NSWindowAbove];
[attachedWindow setAlphaValue:0.0];

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.5];  
[attachedWindow makeKeyAndOrderFront:self];
[[attachedWindow animator] setAlphaValue:1.0];
[NSAnimationContext endGrouping];

Works just fine.

like image 108
sudo rm -rf Avatar answered Nov 10 '22 02:11

sudo rm -rf


I'm not especially well versed in CoreAnimation and the usage of implicit animations. However, I was able to get the MAAttachedWindow to fade in by adding an explicit alphaValue property to the MAAttachedWindow class:

@interface MAAttachedWindow : NSWindow {
    CGFloat _alphaValue;
...
}
-(CGFloat) alphaValue;
-(void) setAlphaValue:(CGFloat)windowAlpha;
...

@implementation MAAttachedWindow

- (CGFloat) alphaValue {
 return _alphaValue;
}

- (void) setAlphaValue:(CGFloat)windowAlpha {
    _alpha = windowAlpha;
 [super setAlphaValue:windowAlpha];
}
...

By adding that, I was able to get the implicit animation for setAlphaValue to work:

(below code cribbed from Matt's Sample "NSStatusItemTest" code)

- (void)toggleAttachedWindowAtPoint:(NSPoint)pt
{
...
    [attachedWindow makeKeyAndOrderFront:self];
 [[attachedWindow animator] setAlphaValue:1.0];

I am not sure why explicitly defining the alphaValue property works. I would expect the inherited version from NSWindow would be invoked for the implicit animation. It doesn't appear to though.

like image 23
Michael Lamb Avatar answered Nov 10 '22 03:11

Michael Lamb