Skip to content

Commit

Permalink
Fix more target content offset edge cases (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryankeller authored Feb 16, 2024
1 parent 27f75a0 commit c62ba8a
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions MagazineLayout/Public/MagazineLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,21 @@ public final class MagazineLayout: UICollectionViewLayout {

// Calculate the target offset before applying updates, since the target offset should be based
// on the pre-update state.
targetContentOffsetAnchor = targetContentOffsetAnchor(
let proposedTargetContentOffsetAnchor = targetContentOffsetAnchor(
bounds: currentCollectionView.bounds,
contentHeight: collectionViewContentSize.height,
topInset: contentInset.top,
bottomInset: contentInset.bottom)
switch proposedTargetContentOffsetAnchor {
case .top, .bottom:
targetContentOffsetAnchor = proposedTargetContentOffsetAnchor
case .topItem, .bottomItem:
// Preserving the content offset when we're not fully at the top or bottom is tricky when
// inserting / deleting items, so we don't even attempt to do it for now.
break
case .none:
break
}

modelState.applyUpdates(updates)
hasDataSourceCountInvalidationBeforeReceivingUpdateItems = false
Expand Down Expand Up @@ -728,12 +738,10 @@ public final class MagazineLayout: UICollectionViewLayout {

let isScrolling = currentCollectionView.isDragging || currentCollectionView.isDecelerating
let top = currentCollectionView.bounds.minY + contentInset.top
let bottom = currentCollectionView.bounds.maxY
let isSizingElementAboveTopEdge = originalAttributes.frame.minY < top
let isSizingElementBelowBottomEdge = originalAttributes.frame.minY > bottom

let contentOffsetAdjustment: CGPoint
if verticalLayoutDirection == .topToBottom, isSizingElementAboveTopEdge {
if isSizingElementAboveTopEdge, isScrolling {
// If layout information is discarded above our current scroll position (on rotation, for
// example), we need to compensate for preferred size changes to items as we're scrolling up,
// otherwise, the collection view will appear to jump each time an element is sized.
Expand All @@ -760,11 +768,7 @@ public final class MagazineLayout: UICollectionViewLayout {
x: 0,
y: preferredAttributes.size.height - originalAttributes.size.height)
}
} else if
verticalLayoutDirection == .bottomToTop,
!isSizingElementBelowBottomEdge,
modelState.isPerformingBatchUpdates
{
} else if targetContentOffsetAnchor == .bottom {
contentOffsetAdjustment = CGPoint(
x: 0,
y: preferredAttributes.size.height - originalAttributes.size.height)
Expand Down

0 comments on commit c62ba8a

Please sign in to comment.