From b0ebcbc3303cae911cb819c1d4b02fcc85463206 Mon Sep 17 00:00:00 2001 From: Bryan Keller Date: Thu, 8 Feb 2024 14:12:17 -0800 Subject: [PATCH] Revert "Fix target content offset edge case (#115)" This reverts commit c3f346b9c80185208fc1e061a6d01a277b27dc01. --- MagazineLayout/Public/MagazineLayout.swift | 54 +++++----------------- 1 file changed, 12 insertions(+), 42 deletions(-) diff --git a/MagazineLayout/Public/MagazineLayout.swift b/MagazineLayout/Public/MagazineLayout.swift index aacae42..a7a8d28 100755 --- a/MagazineLayout/Public/MagazineLayout.swift +++ b/MagazineLayout/Public/MagazineLayout.swift @@ -265,6 +265,8 @@ public final class MagazineLayout: UICollectionViewLayout { supplementaryViewLayoutAttributesForPendingAnimations.removeAll() targetContentOffsetAnchor = nil + preInvalidationContentSize = nil + preInvalidationContentInset = nil super.finalizeCollectionViewUpdates() } @@ -272,25 +274,17 @@ public final class MagazineLayout: UICollectionViewLayout { override public func prepare(forAnimatedBoundsChange oldBounds: CGRect) { super.prepare(forAnimatedBoundsChange: oldBounds) - let contentSize: CGSize - let contentInset: UIEdgeInsets - if let metricsSnapshot = metricsSnapshots.first(where: { $0.bounds == oldBounds }) { - contentSize = metricsSnapshot.contentSize - contentInset = metricsSnapshot.contentInset - } else { - contentSize = collectionViewContentSize - contentInset = self.contentInset - } - targetContentOffsetAnchor = targetContentOffsetAnchor( bounds: oldBounds, - contentHeight: contentSize.height, - topInset: contentInset.top, - bottomInset: contentInset.bottom) + contentHeight: preInvalidationContentSize?.height ?? collectionViewContentSize.height, + topInset: preInvalidationContentInset?.top ?? contentInset.top, + bottomInset: preInvalidationContentInset?.bottom ?? contentInset.bottom) } override public func finalizeAnimatedBoundsChange() { targetContentOffsetAnchor = nil + preInvalidationContentSize = nil + preInvalidationContentInset = nil super.finalizeAnimatedBoundsChange() } @@ -760,29 +754,10 @@ public final class MagazineLayout: UICollectionViewLayout { return } - // We need to save a few content size and content inset values for different bounds. This - // allows us to compute the correct target content offset in `prepareForAnimatedBoundsChange`. - // The root issue is that in the aforementioned function, we need a way to know what the content - // size and content inset _were_ for a given bounds value. - let metricsSnapshot = MetricsSnapshot( - bounds: currentCollectionView.bounds, - contentSize: collectionViewContentSize, - contentInset: lastContentInset ?? contentInset) - - // Replace existing snapshot if the bounds is the same - let indexOfExistingMetricsSnapshot = metricsSnapshots.firstIndex { - $0.bounds == metricsSnapshot.bounds - } - if let indexOfExistingMetricsSnapshot { - metricsSnapshots[indexOfExistingMetricsSnapshot] = metricsSnapshot - } else { - metricsSnapshots.append(metricsSnapshot) - } - - // Limit to 3 snapshots - if metricsSnapshots.count > 3 { - metricsSnapshots.removeFirst() + if collectionViewContentSize.width > 0, collectionViewContentSize.height > 0 { + preInvalidationContentSize = preInvalidationContentSize ?? collectionViewContentSize } + preInvalidationContentInset = preInvalidationContentInset ?? lastContentInset ?? contentInset let shouldInvalidateLayoutMetrics = !context.invalidateEverything && !context.invalidateDataSourceCounts @@ -856,13 +831,8 @@ public final class MagazineLayout: UICollectionViewLayout { private var lastContentInset: UIEdgeInsets? private var cachedCollectionViewWidth: CGFloat? - - private struct MetricsSnapshot { - let bounds: CGRect - let contentSize: CGSize - let contentInset: UIEdgeInsets - } - private var metricsSnapshots = [MetricsSnapshot]() + private var preInvalidationContentSize: CGSize? + private var preInvalidationContentInset: UIEdgeInsets? // These properties are used to prevent scroll jumpiness due to self-sizing after rotation; see // comment in `invalidationContext(forPreferredLayoutAttributes:withOriginalAttributes:)` for more