-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader
More file actions
74 lines (58 loc) · 2.45 KB
/
Loader
File metadata and controls
74 lines (58 loc) · 2.45 KB
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
70
71
72
73
74
import Foundation
import UIKit
class Loader {
static var instance: Loader?
static var isLoading: Bool = false
var activityIndicator: UIActivityIndicatorView?
var loaderView: UIView?
weak var destinationView: UIView?
init(destinationView: UIView) {
self.destinationView = destinationView
Loader.instance = self
}
static func configureDestinationView(destinationView: UIView) {
_ = Loader.init(destinationView: destinationView)
}
static func show() {
guard let self = Loader.instance, let destinationView = self.destinationView, self.loaderView == nil else { return }
Loader.show(destinationView: destinationView)
}
static func show(destinationView: UIView, backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.1), withCenterPercentualOffset centerOffset: Float = 0) {
guard let self = Loader.instance else { return }
// Create the main loader view
if self.loaderView == nil {
self.loaderView = UIView.init(frame: destinationView.frame)
guard let loaderView = self.loaderView else { return }
loaderView.backgroundColor = backgroundColor
}
guard let loaderView = self.loaderView else { return }
self.activityIndicator = UIActivityIndicatorView(style: .large)
if let activityIndicator = self.activityIndicator {
activityIndicator.color = UIColor.lightGray
let offsetPercent = ((loaderView.frame.size.height/2) * CGFloat(centerOffset)) / 100
let center = CGPoint.init(x: loaderView.center.x , y: loaderView.center.y + offsetPercent)
activityIndicator.center = center
loaderView.addSubview(activityIndicator)
activityIndicator.startAnimating()
}
isLoading = true
destinationView.addSubview(loaderView)
}
static func hide() {
// Remove the loadView to free up memory
guard Thread.isMainThread else {
DispatchQueue.main.async {
self.hide()
}
return
}
guard let self = Loader.instance else { return }
DispatchQueue.main.async {
self.activityIndicator?.stopAnimating()
self.activityIndicator?.removeFromSuperview()
self.loaderView?.removeFromSuperview()
self.loaderView = nil
isLoading = false
}
}
}