Cocoa: NSView Animation

岁酱吖の 提交于 2019-11-27 11:35:35

问题


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

I checked the viewDict and theAnim with NSLogs 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.

I'm using Xcode 5.1.1


回答1:


The modern approach is much easier:

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


来源:https://stackoverflow.com/questions/25456080/cocoa-nsview-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!