-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivityIndicator.swift
68 lines (53 loc) · 1.96 KB
/
ActivityIndicator.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
//
// ActivityIndicator.swift
// Allplay
//
// Created by Ruslan Sabirov on 2/15/17.
// Copyright © 2017 Ruslan Sabirov. All rights reserved.
//
import Foundation
import UIKit
class ActivityIndicator: UIView {
static let handler = ActivityIndicator()
let alertWindow = UIApplication.shared.keyWindow!
let loadingView: UIView = UIView()
let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
var isShown = false
func rotated() {
if isShown {
loadingView.center = alertWindow.center
alertWindow.layoutSubviews()
alertWindow.layoutIfNeeded()
}
}
func showActivityIndicator() {
NotificationCenter.default.addObserver(self, selector: #selector(ActivityIndicator.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
if isShown {
} else {
show()
}
}
func hideActivityIndicator() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
hide()
}
private func show() {
loadingView.frame = CGRect(x: 0, y: 0, width: 80, height: 80)
loadingView.center = alertWindow.center
loadingView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10
actInd.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0);
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
actInd.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2)
loadingView.addSubview(actInd)
alertWindow.addSubview(loadingView)
actInd.startAnimating()
isShown = true
}
private func hide() {
loadingView.removeFromSuperview()
actInd.stopAnimating()
isShown = false
}
}