Skip to content

Instantly share code, notes, and snippets.

@bill350
Last active April 3, 2018 14:42
Show Gist options
  • Save bill350/8991ae109ab41ccd2e02cc53ff701b13 to your computer and use it in GitHub Desktop.
Save bill350/8991ae109ab41ccd2e02cc53ff701b13 to your computer and use it in GitHub Desktop.
// MARK: - ErrorDisplayable
extension ErrorView: ErrorDisplayable {
static func show<T: ErrorView>(
fromViewController viewController: UIViewController,
animated: Bool = true,
completion: ((Bool) -> Swift.Void)? = nil) -> T {
guard let subview = loadFromNib() as? T else {
fatalError("The subview is expected to be of type \(T.self)")
}
viewController.view.addSubview(subview)
// Configure constraints if needed
subview.alpha = 0
subview.superview?.sendSubview(toBack: subview)
subview.show(animated: animated) { finished in
if finished {
subview.playAnimation()
}
}
return subview
}
static func show<T: ErrorView>(
fromView view: UIView,
insets: UIEdgeInsets = UIEdgeInsets.zero,
animated: Bool = true,
completion: ((Bool) -> Swift.Void)? = nil) -> T {
guard let subview = loadFromNib() as? T else {
fatalError("The subview is expected to be of type \(T.self)")
}
view.addSubview(subview)
// Configure constraints if needed
subview.alpha = 0
subview.superview?.sendSubview(toBack: subview)
subview.show(animated: animated) { finished in
if finished {
subview.playAnimation()
}
}
return subview
}
func hide(animated: Bool = true, completion: ((Bool) -> Swift.Void)? = nil) {
self.stopAnimation()
let closure: (Bool) -> Void = { (finished) in
if finished {
self.removeFromSuperview()
}
}
if animated {
UIView.animate(withDuration: self.animationDuration,
delay: 0.25,
animations: { self.alpha = 0 }, completion: { (finished) in
closure(finished)
completion?(finished)
})
} else {
self.alpha = 0
closure(true)
completion?(true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment