This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
/
ScrollingStackViewController.swift
315 lines (241 loc) · 11.6 KB
/
ScrollingStackViewController.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// ScrollingStackViewController.swift
// Pods
//
// Created by Maciej Trybilo on 24/01/2017.
// Copyright © 2017 Just Eat Holding Ltd. All rights reserved.
//
import UIKit
public enum Position {
case start
case end
case index(_: Int)
case after(viewController: UIViewController)
case before(viewController: UIViewController)
}
open class ScrollingStackViewController: UIViewController {
public let scrollView: UIScrollView = UIScrollView()
public let stackViewBackgroundView: UIView = UIView()
public let stackView: UIStackView = UIStackView()
public var spacingColor: UIColor = UIColor.clear {
didSet {
stackViewBackgroundView.backgroundColor = spacingColor
}
}
public var borderColor: UIColor = UIColor.gray {
didSet {
stackViewBackgroundView.layer.borderColor = borderColor.cgColor
}
}
public var borderWidth: CGFloat = 0.5 {
didSet {
stackViewBackgroundView.layer.borderWidth = borderWidth
pinStackView(withBorderWidth: self.borderWidth)
scrollView.layoutIfNeeded()
}
}
static public func defaultAnimate(_ animations: @escaping () -> (), completion: ((Bool) -> Void)?) {
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: [],
animations: animations,
completion: completion)
}
public var animate = ScrollingStackViewController.defaultAnimate
private var viewDidLayoutSubviewsClosure: (() -> Void)?
private var maxOffsetY: CGFloat {
return self.scrollView.contentSize.height - self.scrollView.frame.size.height
}
static public func defaultScrollAnimate(_ animations: @escaping () -> (), completion: ((Bool) -> Void)?) {
UIView.animate(withDuration: 0.75,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.25,
options: [],
animations: animations,
completion: completion)
}
public var scrollAnimate = ScrollingStackViewController.defaultScrollAnimate
override open func viewDidLoad() {
super.viewDidLoad()
scrollView.translatesAutoresizingMaskIntoConstraints = false
stackViewBackgroundView.translatesAutoresizingMaskIntoConstraints = false
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.addSubview(stackViewBackgroundView)
scrollView.addSubview(stackView)
stackView.axis = .vertical
stackViewBackgroundView.backgroundColor = UIColor.clear
let views = ["scrollView" : scrollView,
"stackViewBackgroundView" : stackViewBackgroundView,
] as [String : Any]
var constraints = [NSLayoutConstraint]()
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: [], metrics: nil, views: views)
pinStackView(withBorderWidth: borderWidth)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[stackViewBackgroundView]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[stackViewBackgroundView(==scrollView)]|", options: [], metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
}
override open func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
viewDidLayoutSubviewsClosure?()
}
var stackViewConstraints = [NSLayoutConstraint]()
private func pinStackView(withBorderWidth borderWidth: CGFloat) {
scrollView.removeConstraints(stackViewConstraints)
stackViewConstraints.removeAll()
stackViewConstraints += [stackView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: borderWidth)]
stackViewConstraints += [stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -borderWidth)]
stackViewConstraints += [stackView.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: borderWidth)]
stackViewConstraints += [stackView.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -borderWidth)]
stackViewConstraints += [stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -borderWidth * 2)]
stackViewConstraints.forEach { $0.isActive = true }
}
open func add(viewController: UIViewController) {
insert(viewController: viewController)
}
open func add(viewController: UIViewController, edgeInsets: UIEdgeInsets? = nil) {
insert(viewController: viewController, edgeInsets: edgeInsets)
}
open func insert(viewController: UIViewController, at index: Int) {
insert(viewController: viewController, at: .index(index))
}
open func insert(viewController: UIViewController, edgeInsets: UIEdgeInsets? = nil, at position: Position = .end) {
var insertionIndex: Int?
switch position {
case .start:
insertionIndex = 0
case .end:
insertionIndex = children.count
case .index(let index):
insertionIndex = min(Int(index), children.count)
case .after(let afterViewController):
if let afterViewIndex = arrangedViewOrContainerIndex(for: afterViewController.view) {
insertionIndex = afterViewIndex + 1
} else {
insertionIndex = children.count
}
case .before(let beforeViewController):
if let beforeViewIndex = arrangedViewOrContainerIndex(for: beforeViewController.view) {
insertionIndex = beforeViewIndex
} else {
insertionIndex = children.count
}
}
insert(viewController: viewController, edgeInsets: edgeInsets, at: insertionIndex ?? children.count)
}
open func insert(viewController: UIViewController, edgeInsets: UIEdgeInsets?, at index: Int) {
addChild(viewController)
viewController.didMove(toParent: self)
if let edgeInsets = edgeInsets {
let childView: UIView = viewController.view
let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
childView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(childView)
let constraints = [
childView.topAnchor.constraint(equalTo: containerView.topAnchor, constant: edgeInsets.top),
childView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: edgeInsets.left),
containerView.bottomAnchor.constraint(equalTo: childView.bottomAnchor, constant: edgeInsets.bottom),
containerView.trailingAnchor.constraint(equalTo: childView.trailingAnchor, constant: edgeInsets.right),
]
NSLayoutConstraint.activate(constraints)
stackView.insertArrangedSubview(containerView, at: index)
} else {
stackView.insertArrangedSubview(viewController.view, at: index)
}
}
open func remove(viewController: UIViewController) {
guard let arrangedView = arrangedView(for: viewController) else { return }
arrangedView.removeFromSuperview()
if arrangedView != viewController.view {
viewController.view.removeFromSuperview()
}
viewController.willMove(toParent: nil)
viewController.removeFromParent()
}
open func show(viewController: UIViewController,
insertIfNeeded insertion: (position: Position, insets: UIEdgeInsets)? = nil,
_ action: (() -> Void)? = nil) {
if let insertion = insertion, !isArrangedOrContained(view: viewController.view) || !children.contains(viewController) {
insert(viewController: viewController, edgeInsets: insertion.insets, at: insertion.position)
}
animate({
if let view = self.arrangedView(for: viewController) {
view.alpha = 1
view.isHidden = false
}
}, { isFinished in
if isFinished {
action?()
}
})
}
open func hide(viewController: UIViewController, _ action: (() -> Void)? = nil) {
animate({
if let view = self.arrangedView(for: viewController) {
view.alpha = 0
view.isHidden = true
}
}, { isFinished in
if isFinished {
action?()
}
})
}
open func scrollTo(viewController: UIViewController, _ action:(() -> Void)? = nil) {
//check if viewDidLayoutSubviews finished to resize scrollview
if self.isArranged(view: viewController.view), self.maxOffsetY > 0 {
self.scrollTo(view: viewController.view, {
action?()
})
} else if let superview = viewController.view.superview, superview != stackView, self.isArranged(view: superview), self.maxOffsetY > 0 {
self.scrollTo(view: superview, {
action?()
})
} else {
viewDidLayoutSubviewsClosure = { [weak self] in
self?.scrollTo(viewController: viewController, {
self?.viewDidLayoutSubviewsClosure = nil
action?()
})
}
}
}
private func scrollTo(view: UIView, _ finished: @escaping (() -> Void)) {
scrollAnimate({
let offsetY = (view.frame.origin.y >= self.maxOffsetY) ? self.maxOffsetY : view.frame.origin.y
self.scrollView.contentOffset = CGPoint(x: view.frame.origin.x, y: offsetY)
}, { isFinished in
if isFinished {
finished()
}
})
}
public func isArranged(view: UIView) -> Bool {
return arrangedViewIndex(for: view) != nil
}
public func isArrangedOrContained(view: UIView) -> Bool {
return arrangedViewOrContainerIndex(for: view) != nil
}
public func arrangedView(for viewController: UIViewController) -> UIView? {
guard let index = arrangedViewOrContainerIndex(for: viewController.view) else { return nil }
return stackView.arrangedSubviews[index]
}
public func arrangedViewOrContainerIndex(for view: UIView) -> Int? {
return arrangedViewIndex(for: view) ?? arrangedViewContainerIndex(for: view)
}
public func arrangedViewIndex(for view: UIView) -> Int? {
return stackView.arrangedSubviews.index(of: view)
}
public func arrangedViewContainerIndex(for view: UIView) -> Int? {
if let containerView = stackView.arrangedSubviews.first(where: { $0.subviews.contains(view) }) {
return stackView.arrangedSubviews.index(of: containerView)
} else {
return nil
}
}
}