Skip to content

Commit 4b079a5

Browse files
Aymenworksmuukii
authored andcommitted
Add the possibility to remove multiple views (#8)
* Add the possibility to remove multiple views * Refactored to align to the current project code style * Update StackScrollView.swift
1 parent 9a1bca9 commit 4b079a5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

StackScrollView/StackScrollView.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,50 @@ open class StackScrollView: UICollectionView, UICollectionViewDataSource, UIColl
148148
}
149149
}
150150

151+
open func remove(views: [UIView], animated: Bool) {
152+
153+
var indicesForRemove: [Int] = []
154+
155+
for view in views {
156+
if let index = self.views.index(of: view) {
157+
indicesForRemove.append(index)
158+
}
159+
}
160+
161+
// It seems that the layout is not updated properly unless the order is aligned.
162+
indicesForRemove.sort(by: >)
163+
164+
for index in indicesForRemove {
165+
self.views.remove(at: index)
166+
}
167+
168+
if animated {
169+
UIView.animate(
170+
withDuration: 0.5,
171+
delay: 0,
172+
usingSpringWithDamping: 1,
173+
initialSpringVelocity: 0,
174+
options: [
175+
.beginFromCurrentState,
176+
.allowUserInteraction,
177+
.overrideInheritedCurve,
178+
.overrideInheritedOptions,
179+
.overrideInheritedDuration
180+
],
181+
animations: {
182+
self.performBatchUpdates({
183+
self.deleteItems(at: indicesForRemove.map { IndexPath.init(item: $0, section: 0) })
184+
}, completion: nil)
185+
})
186+
} else {
187+
UIView.performWithoutAnimation {
188+
performBatchUpdates({
189+
self.deleteItems(at: indicesForRemove.map { IndexPath.init(item: $0, section: 0) })
190+
}, completion: nil)
191+
}
192+
}
193+
}
194+
151195
open func scroll(to view: UIView, animated: Bool) {
152196

153197
let targetRect = view.convert(view.bounds, to: self)

0 commit comments

Comments
 (0)