Cocoa:NSView动画

Elb*_*mio 27 cocoa fade nsviewanimation

这很简单,所以我不能为我的生活找到什么是错的,我通过文档作为指导,但它仍然无法正常工作.我在一个更大的视图中有一个视图.IBAction应该淡出内部视图......就是这样.这是我得到的:

NSViewAnimation *theAnim;
NSMutableDictionary *viewDict;

// Create the attributes dictionary for the view.
viewDict = [NSMutableDictionary dictionaryWithCapacity:2];

// Set the target object to be the view.
[viewDict setObject:_innerView forKey:NSViewAnimationTargetKey];

// Set this view to fade out
[viewDict setObject:NSViewAnimationFadeOutEffect forKey:NSViewAnimationEffectKey];

theAnim = [[NSViewAnimation alloc] initWithViewAnimations:@[viewDict]];

// Set some additional attributes for the animation.
[theAnim setDuration:1.0];

// Run the animation.
[theAnim startAnimation];
Run Code Online (Sandbox Code Playgroud)

我用NSLogs检查了viewDict和theAnim,两者都没有.我几乎从我工作过的旧程序中复制了这个,现在找不到什么问题.

我正在使用xcode 5.1.1,感谢您的帮助.

Ken*_*ses 70

现代方法更容易:

[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
    context.duration = 1;
    view.animator.alphaValue = 0;
}
completionHandler:^{
    view.hidden = YES;
    view.alphaValue = 1;
}];
Run Code Online (Sandbox Code Playgroud)

如果视图层次结构是图层支持的,那么实际上就足够了:

view.animator.hidden = YES;
Run Code Online (Sandbox Code Playgroud)

  • 为了解释这个差异,这个是Core Animation,但是在AppKit视图中使用animator代理来构建可动画的属性.OP正在使用旧的前核心动画内容,这通常效率低得多. (5认同)

Pau*_*tos 9

对于那些正在寻找Swift版本的人:

NSAnimationContext.runAnimationGroup({ context in
    context.duration = 1
    self.view.animator().alphaValue = 0
}, completionHandler: {
    self.view.isHidden = true
    self.view.alphaValue = 1
})
Run Code Online (Sandbox Code Playgroud)

对于层支持的视图,这就足够了:

view.animator().isHidden = true
Run Code Online (Sandbox Code Playgroud)

对于使用Swift 5.3或更高版本的用户,您可以在此处利用新的Multiple Trailing Closures语法糖来获得更简洁的版本:

NSAnimationContext.runAnimationGroup { context in
    context.duration = 1
    self.view.animator().alphaValue = 0
} completionHandler: {
    self.view.isHidden = true
    self.view.alphaValue = 1
}
Run Code Online (Sandbox Code Playgroud)