Created
December 21, 2020 13:51
-
-
Save levantAJ/1cebedd786e76081f19d5f986f35531b to your computer and use it in GitHub Desktop.
Set UIView hidden with fade animated
This file contains hidden or 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
extension UIView { | |
func setHidden(_ isHidden: Bool, animated: Bool, completion: (() -> Void)? = nil) { | |
if animated { | |
let startAlpha: CGFloat | |
let animatingAlpha: CGFloat | |
if isHidden { | |
startAlpha = 1 | |
animatingAlpha = 0 | |
} else { | |
startAlpha = 0 | |
animatingAlpha = 1 | |
} | |
alpha = startAlpha | |
UIView.animate(withDuration: 0.25, delay: 0.1, | |
options: .curveEaseOut, animations: { [weak self] in | |
self?.alpha = animatingAlpha | |
}, completion: { [weak self] _ in | |
self?.isHidden = isHidden | |
self?.alpha = 1.0 | |
completion?() | |
}) | |
} else { | |
self.isHidden = isHidden | |
completion?() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment