/
LoadingViewController.swift
executable file
·72 lines (52 loc) · 1.86 KB
/
LoadingViewController.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
70
71
72
//
// LoadingViewController.swift
// QR Scores
//
// Created by Erick Sanchez on 1/2/19.
// Copyright © 2018 LinnierGames. All rights reserved.
//
import UIKit
class LoadingViewController: UIViewController {
// MARK: - VARS
@IBOutlet private var loadingIndicator: UIActivityIndicatorView!
// MARK: - RETURN VALUES
// MARK: - METHODS
func present() {
let window = UIWindow.applicationAlertWindow
view.alpha = 0
//if the loading is shown for less than 0.25 seconds, the user will not see the loading indicator
Timer.scheduledTimer(withTimeInterval: 0.25, repeats: false) { [weak self] _ in
window.rootViewController = self
guard let unwrappedSelf = self else { return }
UIView.animate(withDuration: 0.15, animations: {
unwrappedSelf.view.alpha = 1
})
}
window.makeKey()
window.isHidden = false
}
func dismiss(completion: @escaping () -> Void) {
let window = UIWindow.applicationAlertWindow
window.isHidden = true
guard let appWindow = UIApplication.shared.delegate!.window! else {
return completion()
}
UIWindow.applicationAlertWindow.rootViewController = nil
appWindow.makeKey()
completion()
}
// MARK: - IBACTIONS
// MARK: - LIFE CYCLE
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let indicator = self.loadingIndicator {
indicator.startAnimating()
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let indicator = self.loadingIndicator {
indicator.stopAnimating()
}
}
}