-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathLoader.swift
69 lines (55 loc) · 2.07 KB
/
Loader.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Loader.swift
// Lockdown
//
// Created by Johnny Lin on 12/12/19.
// Copyright © 2019 Confirmed Inc. All rights reserved.
//
// https://david.y4ng.fr/simple-hud-with-swift-protocols/
import Foundation
import UIKit
protocol Loadable {
func showLoadingView()
func hideLoadingView()
}
final class LoadingView: UIView {
private let activityIndicatorView = UIActivityIndicatorView(style: .large)
override func layoutSubviews() {
super.layoutSubviews()
backgroundColor = UIColor.black.withAlphaComponent(0.6)
layer.cornerRadius = 5
if activityIndicatorView.superview == nil {
addSubview(activityIndicatorView)
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
activityIndicatorView.startAnimating()
}
}
public func animate() {
activityIndicatorView.startAnimating()
}
}
fileprivate struct Constants {
fileprivate static let loadingViewTag = 63342
}
extension Loadable where Self: UIViewController {
func showLoadingView() {
let loadingView = LoadingView()
view.addSubview(loadingView)
loadingView.translatesAutoresizingMaskIntoConstraints = false
loadingView.widthAnchor.constraint(equalToConstant: 100).isActive = true
loadingView.heightAnchor.constraint(equalToConstant: 100).isActive = true
loadingView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
loadingView.animate()
loadingView.tag = Constants.loadingViewTag
}
func hideLoadingView() {
view.subviews.forEach { subview in
if subview.tag == Constants.loadingViewTag {
subview.removeFromSuperview()
}
}
}
}