2

请参阅代码片段。尝试使用 NSViewAnimation 淡入其主窗口。NIB 只有一个窗口/菜单(例如,这个项目几乎直接来自可可应用程序向导)。通过取消选中“启动时可见”,已在 NIB 中修改了该窗口。委托方法 animationShouldStart 永远不会被调用。如果这很重要,我在 xcode 4.2 中使用 10.7。

我基本上只是不明白为什么这不起作用。请给我一些理智。

谢谢

#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
4

1 回答 1

6

这里存在三个问题,其中一个肯定是不明显的。

首先,您的动画委托没有被调用,因为您的委托方法的消息签名不正确,它应该是:

- (BOOL)animationShouldStart:(NSAnimation*) _anim

其次,要淡入窗口,您需要将窗口本身作为 的对象NSViewAnimationTargetKey,而不是其内容视图。

第三,窗口淡入效果仅在窗口在屏幕上时才有效,但 alpha 值为零。

因此,在代码块的顶部,插入以下内容:

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

那应该使动画中的窗口淡入淡出就可以了。但是,请注意,由于您没有更改窗口框架,因此可以将动画字典缩短为:

NSDictionary *fadeInAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                             _window, NSViewAnimationTargetKey,
                             NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
                             nil];
于 2011-07-27T04:30:35.287 回答