Skip to content

Instantly share code, notes, and snippets.

@jonathanduty
Created August 9, 2018 12:24
Show Gist options
  • Save jonathanduty/5d2c47bc7276319762f30a71e3a0762d to your computer and use it in GitHub Desktop.
Save jonathanduty/5d2c47bc7276319762f30a71e3a0762d to your computer and use it in GitHub Desktop.
LoadingView
import Foundation
import UIKit
import Cartography
class LoadingView: UIView {
let activityView = UIActivityIndicatorView(frame: .zero)
let overlayView = UIView(frame: .zero)
override init(frame: CGRect) {
super.init(frame: .zero)
setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
static func show(inView view: UIView ) {
if let _ = findLoading(inView: view) {
return
}
let loading = LoadingView(frame: .zero)
view.addSubview(loading)
loading.frame = view.bounds
}
static func hide(fromView view: UIView) {
guard let loading = findLoading(inView: view) else {
return
}
loading.removeFromSuperview()
}
static func show(inVC vc: UIViewController ) {
if let navVC = vc.navigationController {
LoadingView.show(inVC: navVC)
return
}
show(inView: vc.view)
}
static func hide(fromVC vc: UIViewController) {
if let navVC = vc.navigationController {
LoadingView.hide(fromVC: navVC)
return
}
hide(fromView: vc.view)
}
static func findLoading(inView view: UIView) -> UIView? {
var found: UIView?
view.subviews.forEach { (view) in
if view is LoadingView {
found = view
}
}
return found
}
fileprivate func setup() {
addSubview(overlayView)
addSubview(activityView)
overlayView.backgroundColor = UIColor.black
overlayView.alpha = 0.5
backgroundColor = UIColor.clear
constrain(overlayView,activityView) { overlayView,activityView in
overlayView.edges == overlayView.superview!.edges
activityView.height == 60
activityView.width == 60
activityView.center == activityView.superview!.center
}
activityView.startAnimating()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment