How to fade out a NSView with animation?
For those looking for a Swift version instead:
NSAnimationContext.runAnimationGroup({ context in
context.duration = 1
self.view.animator().alphaValue = 0
}, completionHandler: {
self.view.isHidden = true
self.view.alphaValue = 1
})
For layer-backed views, this is enough:
view.animator().isHidden = true
For those already on Swift 5.3 or above, you can leverage the new Multiple Trailing Closures syntax sugar here for an even cleaner version:
NSAnimationContext.runAnimationGroup { context in
context.duration = 1
self.view.animator().alphaValue = 0
} completionHandler: {
self.view.isHidden = true
self.view.alphaValue = 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;