如何用动画淡出 NSView? [英] How to fade out a NSView with animation?

查看:33
本文介绍了如何用动画淡出 NSView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很简单,所以我一生都找不到问题所在,我查看了文档作为指南,但仍然没有用.我在更大的视图中有一个视图.IBAction 应该淡出内部视图......就是这样.这是我所拥有的:

This is as simple as can be so I can't for the life of me find what's wrong, I looked through the documentation as a guide but it still didn't work. I have a view inside a larger view. An IBAction is supposed to fade out the inner view... that's it. Here's what I've got:

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];

我用 NSLog 检查了 viewDicttheAnim 并且都不是 nil.我几乎是从一个旧程序复制过来的,现在我找不到问题所在.

I checked the viewDict and theAnim with NSLog and neither are nil. I pretty much copied this from an old program I had where this was working, can't find what's wrong now.

我使用的是 Xcode 5.1.1.

I'm using Xcode 5.1.1.

推荐答案

现代方法要容易得多:

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

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

If the view hierarchy is layer-backed, it's actually sufficient to do:

view.animator.hidden = YES;

这篇关于如何用动画淡出 NSView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆