可可:NSView动画

问题描述 投票:27回答:1

这非常简单,所以我一生无法发现问题所在,我仔细阅读了文档作为指导,但仍然无法正常工作。我有一个更大的内部视图。 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];

我用NSLogs检查了viewDict和TheAnim,它们都不为零。我几乎是从一个我有工作的旧程序中复制过来的,现在找不到问题所在。

我正在使用Xcode 5.1.1

cocoa fade nsviewanimation
1个回答
70
投票

现代方法要容易得多:

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

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

view.animator.hidden = YES;
© www.soinside.com 2019 - 2024. All rights reserved.