Skip to content

Instantly share code, notes, and snippets.

@wata
Last active July 25, 2018 11:49
Show Gist options
  • Select an option

  • Save wata/155bb722b8719a0e163029cee520be65 to your computer and use it in GitHub Desktop.

Select an option

Save wata/155bb722b8719a0e163029cee520be65 to your computer and use it in GitHub Desktop.
Animations of FadeIn / FadeOut
// Modified from: https://qiita.com/mono0926/items/c53b2e46e51a0b0bbf06
enum FadeType: TimeInterval {
case normal = 0.2
case slow = 1.0
}
extension UIView {
func fadeIn(_ type: FadeType = .normal, completion: (() -> Void)? = nil) {
self.alpha = 0
self.isHidden = false
UIView.animate(withDuration: type.rawValue, animations: {
self.alpha = 1
}, completion: { _ in
completion?()
})
}
func fadeOut(type: FadeType = .normal, completion: (() -> Void)? = nil) {
UIView.animate(withDuration: type.rawValue, animations: {
self.alpha = 0
}, completion: { [weak self] _ in
self?.isHidden = true
self?.alpha = 1
completion?()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment