Last active
August 7, 2024 12:29
-
-
Save martinhoeller/7a1d41e3744f4191d3b3f38868be5190 to your computer and use it in GitHub Desktop.
An NSStackView extension for animated hiding/showing of views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSStackView+Animations.swift | |
// | |
// Created by Martin Höller on 18.06.16. | |
// Copyright © 2016 blue banana software. All rights reserved. | |
// | |
// Based on http://prod.lists.apple.com/archives/cocoa-dev/2015/Jan/msg00314.html | |
import Cocoa | |
extension NSStackView { | |
func hideViews(views: [NSView], animated: Bool) { | |
views.forEach { view in | |
view.hidden = true | |
} | |
if animated { | |
NSAnimationContext.runAnimationGroup({ context in | |
context.duration = 0.3 | |
context.allowsImplicitAnimation = true | |
self.window?.layoutIfNeeded() | |
}, completionHandler: nil) | |
} | |
} | |
func showViews(views: [NSView], animated: Bool) { | |
views.forEach { view in | |
// unhide the view so the stack view knows how to layout… | |
view.hidden = false | |
if animated { | |
view.wantsLayer = true | |
// …but set opacity to 0 so the view is not visible during the animation | |
view.layer!.opacity = 0.0 | |
} | |
} | |
if animated { | |
NSAnimationContext.runAnimationGroup({ context in | |
context.duration = 0.3 | |
context.allowsImplicitAnimation = true | |
self.window?.layoutIfNeeded() | |
}, completionHandler: { | |
// reset opacity to show the views after the animation finished | |
views.forEach { view in | |
view.layer!.opacity = 1.0 | |
} | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment