zer0gravitas
zer0gravitas

Reputation: 365

failing to fade in window using NSViewAnimation

See the code snippet. Attempting to fade in its main window using NSViewAnimation. The NIB just has a window/menu (eg this project is almost straight from the cocoa app wizard). The window has been modified in the NIB by unchecking "visible at launch". The delegate method animationShouldStart is never called. If it matters at all I am on 10.7 in xcode 4.2.

I am fundamentally just not understanding why this doesn't work. Please knock some sense in to me.

Thanks

#import "TestAppDelegate.h"

@implementation TestAppDelegate

@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
  //  [[self window] orderFront: self];
    NSRect _saveRect = [_window  frame];
    NSRect _zeroRect = _saveRect;
    _zeroRect.size = NSMakeSize(0, 0);   
    NSDictionary *fadeInAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [_window contentView], NSViewAnimationTargetKey,
                                 NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
                                 [NSValue valueWithRect:_zeroRect], NSViewAnimationStartFrameKey,
                                 [NSValue valueWithRect:_saveRect], NSViewAnimationEndFrameKey,
                                 nil];

    NSViewAnimation *_viewAnimIn = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects: fadeInAttrs, nil]];

    [_viewAnimIn setDuration:1.0];
    [_viewAnimIn setAnimationCurve:NSAnimationEaseInOut];  
    [_viewAnimIn setAnimationBlockingMode:NSAnimationBlocking];
    [_viewAnimIn setDelegate:self];
    [_viewAnimIn startAnimation];
}

- (BOOL)animation:(NSAnimation *)animation animationShouldStart:(NSAnimation*) _anim
{
    NSLog(@"%@ shouldStart", _anim);
    return YES;
}

@end

Upvotes: 2

Views: 2116

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46020

There are three problems here, one of which is definitely non-obvious.

Firstly, your animation delegate is not being called because the message signature for your delegate method is incorrect, it should be:

- (BOOL)animationShouldStart:(NSAnimation*) _anim

Secondly, to fade in a window, you need to pass the window itself as the object for the NSViewAnimationTargetKey, not its content view.

Thirdly, the window fade in effect will only work if the window is on-screen, but with an alpha value of zero.

So, at the top of your code block, insert this:

[self.window orderFront:self];
[self.window setAlphaValue:0.0];

That should make the window fade in animation work just fine. However, note that because you're not changing the window frame, you can shorten your animation dictionary to this:

NSDictionary *fadeInAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                             _window, NSViewAnimationTargetKey,
                             NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
                             nil];

Upvotes: 6

Related Questions