Skip to content

Instantly share code, notes, and snippets.

@levantAJ
Created December 21, 2020 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levantAJ/1cebedd786e76081f19d5f986f35531b to your computer and use it in GitHub Desktop.
Save levantAJ/1cebedd786e76081f19d5f986f35531b to your computer and use it in GitHub Desktop.
Set UIView hidden with fade animated
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