Skip to content

Commit 67e1d38

Browse files
authored
Fix jumpy scrolling due to incorrect target content offset calculation (#137)
1 parent a682bda commit 67e1d38

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

MagazineLayout/LayoutCore/ModelState.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ final class ModelState {
105105
return nil
106106
}
107107

108+
func isItemHeightSettled(indexPath: IndexPath) -> Bool {
109+
let item = sectionModels(for: .afterUpdates)[indexPath.section].itemModel(
110+
atIndex: indexPath.item)
111+
switch item.sizeMode.heightMode {
112+
case .static:
113+
return true
114+
case .dynamicAndStretchToTallestItemInRow, .dynamic(_):
115+
return item.preferredHeight != nil
116+
}
117+
}
118+
108119
func itemModelHeightModeDuringPreferredAttributesCheck(
109120
at indexPath: IndexPath)
110121
-> MagazineLayoutItemHeightMode?

MagazineLayout/Public/MagazineLayout.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,11 @@ public final class MagazineLayout: UICollectionViewLayout {
12941294
}
12951295
visibleItemLocationFramePairs.sort { $0.elementLocation < $1.elementLocation }
12961296

1297-
let firstVisibleItemLocationFramePair = visibleItemLocationFramePairs.first
1297+
let firstVisibleItemLocationFramePair = visibleItemLocationFramePairs.first {
1298+
// When scrolling up, only calculate a a target content offset based on visible, already-sized
1299+
// cells. Otherwise, scrolling will be jumpy.
1300+
modelState.isItemHeightSettled(indexPath: $0.elementLocation.indexPath)
1301+
}
12981302
let lastVisibleItemLocationFramePair = visibleItemLocationFramePairs.last
12991303

13001304
guard

0 commit comments

Comments
 (0)