@@ -135,15 +135,16 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
135135
136136 // MARK: Private functions
137137
138- private func updateCurrentPageIfNeeded( ) {
138+ private func updateCurrentPageIfNeeded( basedOn contentOffset : CGPoint ? = nil ) {
139139 var currentPage : Int = 0
140140 if let collectionView = collectionView {
141+ let contentOffset = contentOffset ?? collectionView. contentOffset
141142 let pageSize = scrollDirection == . horizontal ? collectionView. frame. width : collectionView. frame. height
142- let contentOffset = scrollDirection == . horizontal ?
143- ( collectionView . contentOffset. x + collectionView. contentInset. left) :
144- ( collectionView . contentOffset. y + collectionView. contentInset. top)
143+ let offset = scrollDirection == . horizontal ?
144+ ( contentOffset. x + collectionView. contentInset. left) :
145+ ( contentOffset. y + collectionView. contentInset. top)
145146 if pageSize > 0 {
146- currentPage = Int ( round ( contentOffset / pageSize) )
147+ currentPage = Int ( round ( offset / pageSize) )
147148 }
148149 }
149150 if currentPage != self . currentPage {
@@ -174,5 +175,6 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
174175 offset = min ( offset, maxPossibleOffset)
175176 let contentOffset : CGPoint = scrollDirection == . horizontal ? CGPoint ( x: offset, y: 0 ) : CGPoint ( x: 0 , y: offset)
176177 collectionView? . setContentOffset ( contentOffset, animated: animated)
178+ updateCurrentPageIfNeeded ( basedOn: contentOffset)
177179 }
178180}
0 commit comments