forked from hyperoslo/ImagePicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackView.swift
174 lines (140 loc) · 4.43 KB
/
StackView.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import UIKit
import Photos
protocol ImageStackViewDelegate: class {
func imageStackViewDidPress()
}
class ImageStackView: UIView {
struct Dimensions {
static let imageSize: CGFloat = 58
}
weak var delegate: ImageStackViewDelegate?
lazy var activityView: UIActivityIndicatorView = {
let view = UIActivityIndicatorView()
view.alpha = 0.0
return view
}()
var views: [UIImageView] = {
var array = [UIImageView]()
for _ in 0...3 {
let view = UIImageView()
view.layer.cornerRadius = 3
view.layer.borderColor = UIColor.white.cgColor
view.layer.borderWidth = 1
view.contentMode = .scaleAspectFill
view.clipsToBounds = true
view.alpha = 0
array.append(view)
}
return array
}()
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
subscribe()
views.forEach { addSubview($0) }
addSubview(activityView)
views.first?.alpha = 1
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - Helpers
func subscribe() {
NotificationCenter.default.addObserver(self,
selector: #selector(imageDidPush(_:)),
name: NSNotification.Name(rawValue: ImageStack.Notifications.imageDidPush),
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(imageStackDidChangeContent(_:)),
name: NSNotification.Name(rawValue: ImageStack.Notifications.imageDidDrop),
object: nil)
NotificationCenter.default.addObserver(self,
selector: #selector(imageStackDidChangeContent(_:)),
name: NSNotification.Name(rawValue: ImageStack.Notifications.stackDidReload),
object: nil)
}
override func layoutSubviews() {
let step: CGFloat = -3.0
let scale: CGFloat = 0.8
let viewSize = CGSize(width: frame.width * scale,
height: frame.height * scale)
let offset = -step * CGFloat(views.count)
var origin = CGPoint(x: offset, y: offset)
for view in views {
origin.x += step
origin.y += step
view.frame = CGRect(origin: origin, size: viewSize)
}
}
func startLoader() {
if let firstVisibleView = views.filter({ $0.alpha == 1.0 }).last {
activityView.frame.origin.x = firstVisibleView.center.x
activityView.frame.origin.y = firstVisibleView.center.y
}
activityView.startAnimating()
UIView.animate(withDuration: 0.3, animations: {
self.activityView.alpha = 1.0
})
}
}
extension ImageStackView {
@objc func imageDidPush(_ notification: Notification) {
let emptyView = views.filter { $0.image == nil }.first
if let emptyView = emptyView {
animateImageView(emptyView)
}
if let sender = notification.object as? ImageStack {
renderViews(sender.assets)
activityView.stopAnimating()
}
}
@objc func imageStackDidChangeContent(_ notification: Notification) {
if let sender = notification.object as? ImageStack {
renderViews(sender.assets)
activityView.stopAnimating()
}
}
@objc func renderViews(_ assets: [PHAsset]) {
if let firstView = views.first, assets.isEmpty {
views.forEach {
$0.image = nil
$0.alpha = 0
}
firstView.alpha = 1
return
}
let photos = Array(assets.suffix(4))
for (index, view) in views.enumerated() {
if index <= photos.count - 1 {
AssetManager.resolveAsset(photos[index], size: CGSize(width: Dimensions.imageSize, height: Dimensions.imageSize)) { image in
view.image = image
}
view.alpha = 1
} else {
view.image = nil
view.alpha = 0
}
if index == photos.count {
UIView.animate(withDuration: 0.3, animations: {
self.activityView.frame.origin = CGPoint(x: view.center.x + 3, y: view.center.x + 3)
})
}
}
}
fileprivate func animateImageView(_ imageView: UIImageView) {
imageView.transform = CGAffineTransform(scaleX: 0, y: 0)
UIView.animate(withDuration: 0.3, animations: {
imageView.transform = CGAffineTransform(scaleX: 1.05, y: 1.05)
}, completion: { _ in
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.activityView.alpha = 0.0
imageView.transform = CGAffineTransform.identity
}, completion: { _ in
self.activityView.stopAnimating()
})
})
}
}