Skip to content
This repository was archived by the owner on Jun 17, 2023. It is now read-only.

Commit df67411

Browse files
authored
Merge pull request #14 from zzzworm/master
Fix setting `currentPage` with a wrong value when `animated` is true
2 parents 62a5003 + 91b4732 commit df67411

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Lib/CollectionViewPagingLayout.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
6565

6666

6767
// MARK: UICollectionViewLayout
68+
var ignoreUpdateCurrentPage : Bool = false
6869

6970
override public func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
7071
true
@@ -129,7 +130,9 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
129130

130131
override public func invalidateLayout() {
131132
super.invalidateLayout()
132-
updateCurrentPageIfNeeded()
133+
if !ignoreUpdateCurrentPage{
134+
updateCurrentPageIfNeeded()
135+
}
133136
}
134137

135138

@@ -174,7 +177,14 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
174177
offset = max(0, offset)
175178
offset = min(offset, maxPossibleOffset)
176179
let contentOffset: CGPoint = scrollDirection == .horizontal ? CGPoint(x: offset, y: 0) : CGPoint(x: 0, y: offset)
180+
ignoreUpdateCurrentPage = true
181+
CATransaction.begin()
182+
CATransaction.setCompletionBlock({
183+
self.ignoreUpdateCurrentPage = false
184+
self.updateCurrentPageIfNeeded(basedOn: contentOffset)
185+
})
177186
collectionView?.setContentOffset(contentOffset, animated: animated)
178-
updateCurrentPageIfNeeded(basedOn: contentOffset)
187+
CATransaction.commit()
188+
179189
}
180190
}

0 commit comments

Comments
 (0)