Skip to content

Commit 0d7cac6

Browse files
JohnEstropiamuukii
authored andcommitted
add insert(views:) method variants (#10)
1 parent 46d2db1 commit 0d7cac6

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

StackScrollView-Demo/ViewController.swift

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ class ViewController: UIViewController {
9090
views.append(MarginStackCell(height: 40, backgroundColor: marginColor))
9191

9292
views.append(HeaderStackCell(title: "ButtonStackCell", backgroundColor: marginColor))
93-
94-
(0..<3).forEach { _ in
95-
96-
let s = fullSeparator()
97-
93+
94+
let makeRemovableButton: () -> [UIView] = {
95+
96+
let s = self.fullSeparator()
97+
98+
var views: [UIView] = []
9899
views.append(s)
99-
100+
100101
views.append({
101102
let v = ButtonStackCell(buttonTitle: "Remove")
102103
v.tapped = { [unowned v] in
@@ -105,8 +106,28 @@ class ViewController: UIViewController {
105106
}
106107
return v
107108
}())
108-
109+
return views
109110
}
111+
112+
views.append(contentsOf: { () -> [UIView] in
113+
let s = fullSeparator()
114+
let v = ButtonStackCell(buttonTitle: "Insert Before")
115+
v.tapped = { [unowned stackScrollView, unowned s] in
116+
let views = (0 ... .random(in: 1 ... 2)).flatMap { _ in makeRemovableButton() }
117+
stackScrollView.insert(views: views, before: s, animated: true)
118+
}
119+
return [s, v]
120+
}())
121+
122+
views.append(contentsOf: { () -> [UIView] in
123+
let s = fullSeparator()
124+
let v = ButtonStackCell(buttonTitle: "Insert After")
125+
v.tapped = { [unowned stackScrollView, unowned v] in
126+
let views = (0 ... .random(in: 1 ... 2)).flatMap { _ in makeRemovableButton() }
127+
stackScrollView.insert(views: views, after: v, animated: true)
128+
}
129+
return [s, v]
130+
}())
110131

111132
views.append(fullSeparator())
112133

StackScrollView/StackScrollView.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,56 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
112112
func append(lazy: @escaping () -> UIView) {
113113

114114
}
115+
116+
open func insert(views _views: [UIView], at index: Int, animated: Bool) {
117+
118+
var _views = _views
119+
_views.removeAll(where: views.contains(_:))
120+
views.insert(contentsOf: _views, at: index)
121+
_views.forEach { view in
122+
register(Cell.self, forCellWithReuseIdentifier: identifier(view))
123+
}
124+
let batchUpdates: () -> Void = {
125+
self.performBatchUpdates({
126+
self.insertItems(at: (index ..< index.advanced(by: _views.count)).map({ IndexPath(item: $0, section: 0) }))
127+
}, completion: nil)
128+
}
129+
if animated {
130+
UIView.animate(
131+
withDuration: 0.5,
132+
delay: 0,
133+
usingSpringWithDamping: 1,
134+
initialSpringVelocity: 0,
135+
options: [
136+
.beginFromCurrentState,
137+
.allowUserInteraction,
138+
.overrideInheritedCurve,
139+
.overrideInheritedOptions,
140+
.overrideInheritedDuration
141+
],
142+
animations: batchUpdates,
143+
completion: nil)
144+
145+
} else {
146+
UIView.performWithoutAnimation(batchUpdates)
147+
}
148+
}
149+
150+
open func insert(views _views: [UIView], before view: UIView, animated: Bool) {
151+
152+
guard let index = views.firstIndex(of: view) else {
153+
return
154+
}
155+
insert(views: _views, at: index, animated: animated)
156+
}
157+
158+
open func insert(views _views: [UIView], after view: UIView, animated: Bool) {
159+
160+
guard let index = views.firstIndex(of: view)?.advanced(by: 1) else {
161+
return
162+
}
163+
insert(views: _views, at: index, animated: animated)
164+
}
115165

116166
open func remove(view: UIView, animated: Bool) {
117167

0 commit comments

Comments
 (0)