请参阅代码片段。尝试使用NSViewAnimation淡入其主窗口。NIB只有一个窗口/菜单(例如,这个项目几乎直接来自可可应用向导)。已在NIB中修改该窗口,方法是取消选中“启动时可见”。永远不会调用委托方法animationShouldStart。如果重要的话,我是在Xcode4.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发布于 2011-07-27 12:30:35
这里有三个问题,其中一个肯定是不明显的。
首先,您的动画委托没有被调用,因为您的委托方法的消息签名不正确,它应该是:
- (BOOL)animationShouldStart:(NSAnimation*) _anim其次,要淡入窗口,需要将窗口本身作为NSViewAnimationTargetKey的对象传递,而不是作为其内容视图。
第三,只有当窗口在屏幕上,但alpha值为零时,窗口淡入效果才有效。
因此,在代码块的顶部插入以下内容:
[self.window orderFront:self];
[self.window setAlphaValue:0.0];这应该会使窗口在动画中淡入淡出状态。但是,请注意,因为您没有更改窗口框架,所以可以将动画字典缩短为:
NSDictionary *fadeInAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
_window, NSViewAnimationTargetKey,
NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
nil];https://stackoverflow.com/questions/6836827
复制相似问题