-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubViewLoadingProtocol.swift
More file actions
125 lines (104 loc) · 5.35 KB
/
SubViewLoadingProtocol.swift
File metadata and controls
125 lines (104 loc) · 5.35 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// SubViewLoadingProtocol.swift
// SubLoadingViewProtocolTest
//
// Created by hanwe lee on 2021/03/09.
//
import UIKit
protocol SubViewLoadingProtocol {
var subViewLoadingView: UIImageView { get set }
var subViewLoadingViewSize: CGSize { get set }
var subViewLoadingViewBackgroundViewColor: UIColor { get set }
func showSubLoadingView(view: UIView, fadeInDuration: TimeInterval, completion: (() -> ())?)
func hideSubLoadingView(view: UIView, fadeOutDuration: TimeInterval, completion: (() -> ())?)
}
extension SubViewLoadingProtocol {
func showSubLoadingView(view: UIView, fadeInDuration: TimeInterval ,completion: (() -> ())?) {
let containerView: SubLoadingContainerView = SubLoadingContainerView()
containerView.loadingViewSize = self.subViewLoadingViewSize
containerView.imageView = self.subViewLoadingView
containerView.makeLoadingView()
containerView.backgroundColor = self.subViewLoadingViewBackgroundViewColor
view.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
let constraint1 = NSLayoutConstraint(item: containerView, attribute: .leading, relatedBy: .equal,
toItem: view, attribute: .leading,
multiplier: 1.0, constant: 0)
let constraint2 = NSLayoutConstraint(item: containerView, attribute: .trailing, relatedBy: .equal,
toItem: view, attribute: .trailing,
multiplier: 1.0, constant: 0)
let constraint3 = NSLayoutConstraint(item: containerView, attribute: .top, relatedBy: .equal,
toItem: view, attribute: .top,
multiplier: 1.0, constant: 0)
let constraint4 = NSLayoutConstraint(item: containerView, attribute: .bottom, relatedBy: .equal,
toItem: view, attribute: .bottom,
multiplier: 1.0, constant: 0)
view.addConstraints([constraint1,constraint2,constraint3,constraint4])
containerView.showLoading(fadeInDuration: fadeInDuration, completion: completion)
}
func hideSubLoadingView(view: UIView, fadeOutDuration: TimeInterval, completion: (() -> ())?) {
let subViews = view.subviews.filter{
return $0 is SubLoadingContainerView
}
for i in 0..<subViews.count {
(subViews[i] as? SubLoadingContainerView)?.hideLoading(fadeOutDuration: fadeOutDuration ,completion: {
subViews[i].removeFromSuperview()
completion?()
})
}
}
}
fileprivate class SubLoadingContainerView: UIView {
var imageView: UIImageView? = nil
var loadingViewSize: CGSize? = nil
func showLoading(fadeInDuration: TimeInterval = 0.0, completion: (() -> ())?) {
fadeIn(duration: fadeInDuration, completion: {
self.imageView?.startAnimating()
completion?()
})
}
func hideLoading(fadeOutDuration: TimeInterval = 0.0, completion: (() -> ())?) {
self.imageView?.stopAnimating()
fadeOut(duration: fadeOutDuration, completion: {
completion?()
})
}
func makeLoadingView() {
guard let loadingView = self.imageView else {
print("imageView is not exsit")
return
}
guard let size = self.loadingViewSize else {
print("loadingViewSize is not exsit")
return
}
self.addSubview(loadingView)
loadingView.translatesAutoresizingMaskIntoConstraints = false
let loadingViewConstraint1 = NSLayoutConstraint(item: loadingView, attribute: .centerX, relatedBy: .equal,
toItem: self, attribute: .centerX,
multiplier: 1.0, constant: 0)
let loadingViewConstraint2 = NSLayoutConstraint(item: loadingView, attribute: .centerY, relatedBy: .equal,
toItem: self, attribute: .centerY,
multiplier: 1.0, constant: 0)
let loadingViewConstraint3 = loadingView.widthAnchor.constraint(equalToConstant: size.width)
let loadingViewConstraint4 = loadingView.heightAnchor.constraint(equalToConstant: size.height)
self.addConstraints([loadingViewConstraint1,loadingViewConstraint2,loadingViewConstraint3,loadingViewConstraint4])
}
fileprivate func fadeIn(duration: TimeInterval, completion: @escaping () -> ()) {
self.alpha = 0.0
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseIn, animations: {
self.alpha = 1.0
}, completion: { (finished: Bool) -> Void in
self.imageView?.startAnimating()
completion()
})
}
fileprivate func fadeOut(duration: TimeInterval, completion: @escaping () -> ()) {
self.alpha = 1.0
UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: {
self.alpha = 0.0
}, completion: { (finished: Bool) -> Void in
completion()
})
}
}