-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressHUD.swift
80 lines (67 loc) · 2.58 KB
/
ProgressHUD.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
73
74
75
76
77
78
79
80
//
// ProgressHUD.swift
// SwiftLibrary
//
// Created by 劉瑞元 on 2022/5/10.
//
import Foundation
import UIKit
class ProgressHUD: UIVisualEffectView {
let label: UILabel = UILabel()
var text: String? {
didSet {
label.text = text
}
}
let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let vibracyView: UIVisualEffectView
init(text: String) {
self.text = text
self.vibracyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect))
super.init(effect: blurEffect)
self.setup()
}
// This require init section is for the interface builder(Storyboard) using
required init(coder aDecoder: NSCoder) {
self.text = ""
self.vibracyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect))
super.init(coder: aDecoder)!
self.setup()
}
func setup() {
contentView.addSubview(vibracyView)
vibracyView.contentView.addSubview(activityIndicator)
vibracyView.contentView.addSubview(label)
activityIndicator.color = UIColor.white
activityIndicator.startAnimating()
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
if let superView = self.superview {
let width = superView.frame.size.width / 1.5
let height: CGFloat = 50.0
self.frame = CGRect(x: superView.frame.size.width / 2 - width / 2,
y: superView.frame.height / 2 - height / 2,
width: width,
height: height
)
vibracyView.frame = self.bounds
let activityIndicatorSize: CGFloat = 40.0
activityIndicator.frame = CGRect(x: 5, y: height / 2 - activityIndicatorSize / 2, width: activityIndicatorSize, height: activityIndicatorSize)
layer.cornerRadius = 8.0
layer.masksToBounds = true
label.text = text
label.textAlignment = NSTextAlignment.center
label.frame = CGRect(x: activityIndicatorSize + 5, y: 0, width: width - activityIndicatorSize - 15, height: height)
label.textColor = UIColor.blue
label.font = UIFont.boldSystemFont(ofSize: 16.0)
}
}
func show() {
self.isHidden = false
}
func hide() {
self.isHidden = true
}
}