Skip to content

Commit 7b7ebd6

Browse files
authored
Merge pull request #28 from yacir/optimization
Invalidate the cache for bounds only if the size has changed
2 parents 4f6eb60 + a6903e8 commit 7b7ebd6

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

Sources/CollectionViewSlantedLayout.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ import UIKit
144144
internal var cachedAttributes = [CollectionViewSlantedLayoutAttributes]()
145145
/// :nodoc:
146146
internal var cachedContentSize: CGFloat = 0
147+
/// :nodoc:
148+
internal var currentSize: CGSize = CGSize.zero
147149

148150
/// :nodoc:
149151
fileprivate func itemSize(forItemAt indexPath: IndexPath) -> (value: CGFloat, isDynamic: Bool) {
@@ -215,6 +217,7 @@ import UIKit
215217
cachedAttributes = [CollectionViewSlantedLayoutAttributes]()
216218
cachedContentSize = 0
217219
}
220+
218221
}
219222

220223
// MARK: CollectionViewLayout methods overriding
@@ -231,6 +234,9 @@ extension CollectionViewSlantedLayout {
231234

232235
/// :nodoc:
233236
override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
237+
guard newBounds.size != currentSize else {
238+
return false
239+
}
234240
invalidateCache()
235241
return true
236242
}
@@ -241,8 +247,8 @@ extension CollectionViewSlantedLayout {
241247
return
242248
}
243249

250+
currentSize = collectionView?.bounds.size ?? CGSize.zero
244251
let staticMasks = calculatedMasks(itemSize: max(itemSize, 0))
245-
246252
var position: CGFloat = 0
247253

248254
for item in 0..<numberOfItems {

0 commit comments

Comments
 (0)