@@ -43,13 +43,9 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
43
43
}
44
44
45
45
private var numberOfItems : Int {
46
- var count = 0 ;
47
- if let sectionCount = collectionView? . numberOfSections {
48
- for i in ( 0 ..< sectionCount) {
49
- count += collectionView? . numberOfItems ( inSection: i) ?? 0
50
- }
51
- }
52
- return count
46
+ ( 0 ..< ( collectionView? . numberOfSections ?? 0 ) )
47
+ . compactMap { collectionView? . numberOfItems ( inSection: $0) }
48
+ . reduce ( 0 , + )
53
49
}
54
50
55
51
@@ -76,6 +72,7 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
76
72
77
73
override public func layoutAttributesForElements( in rect: CGRect ) -> [ UICollectionViewLayoutAttributes ] ? {
78
74
let currentScrollOffset = self . currentScrollOffset
75
+ let numberOfItems = self . numberOfItems
79
76
let attributesCount = numberOfVisibleItems ?? numberOfItems
80
77
let visibleRangeMid = attributesCount / 2
81
78
let currentPageIndex = Int ( round ( currentScrollOffset) )
@@ -94,23 +91,19 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
94
91
let endIndex = min ( numberOfItems, initialEndIndex + startIndexOutOfBounds)
95
92
96
93
var attributesArray : [ UICollectionViewLayoutAttributes ] = [ ]
97
- let sectionCount = collectionView? . numberOfSections ?? 0
94
+ var section = 0
95
+ var numberOfItemsInSection = collectionView? . numberOfItems ( inSection: section) ?? 0
96
+ var numberOfItemsInPrevSections = 0
98
97
for index in startIndex..< endIndex {
99
- var section = 0
100
- var row = 0
101
- var accIndex = 0
102
- for i in ( 0 ..< sectionCount) {
103
- if index - accIndex < collectionView!. numberOfItems ( inSection: i) {
104
- section = i
105
- row = index - accIndex
106
- break ;
107
- }
108
- else {
109
- accIndex += collectionView!. numberOfItems ( inSection: i)
110
- }
98
+ var item = index - numberOfItemsInPrevSections
99
+ while item >= numberOfItemsInSection {
100
+ numberOfItemsInPrevSections += numberOfItemsInSection
101
+ section += 1
102
+ numberOfItemsInSection = collectionView? . numberOfItems ( inSection: section) ?? 0
103
+ item = index - numberOfItemsInPrevSections
111
104
}
112
-
113
- let cellAttributes = UICollectionViewLayoutAttributes ( forCellWith: IndexPath ( row : row , section: section) )
105
+
106
+ let cellAttributes = UICollectionViewLayoutAttributes ( forCellWith: IndexPath ( item : item , section: section) )
114
107
let pageIndex = CGFloat ( index)
115
108
let progress = pageIndex - currentScrollOffset
116
109
var zIndex = Int ( - abs( round ( progress) ) )
@@ -142,15 +135,16 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
142
135
143
136
// MARK: Private functions
144
137
145
- private func updateCurrentPageIfNeeded( ) {
138
+ private func updateCurrentPageIfNeeded( basedOn contentOffset : CGPoint ? = nil ) {
146
139
var currentPage : Int = 0
147
140
if let collectionView = collectionView {
141
+ let contentOffset = contentOffset ?? collectionView. contentOffset
148
142
let pageSize = scrollDirection == . horizontal ? collectionView. frame. width : collectionView. frame. height
149
- let contentOffset = scrollDirection == . horizontal ?
150
- ( collectionView . contentOffset. x + collectionView. contentInset. left) :
151
- ( collectionView . contentOffset. y + collectionView. contentInset. top)
143
+ let offset = scrollDirection == . horizontal ?
144
+ ( contentOffset. x + collectionView. contentInset. left) :
145
+ ( contentOffset. y + collectionView. contentInset. top)
152
146
if pageSize > 0 {
153
- currentPage = Int ( round ( contentOffset / pageSize) )
147
+ currentPage = Int ( round ( offset / pageSize) )
154
148
}
155
149
}
156
150
if currentPage != self . currentPage {
@@ -181,5 +175,6 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
181
175
offset = min ( offset, maxPossibleOffset)
182
176
let contentOffset : CGPoint = scrollDirection == . horizontal ? CGPoint ( x: offset, y: 0 ) : CGPoint ( x: 0 , y: offset)
183
177
collectionView? . setContentOffset ( contentOffset, animated: animated)
178
+ updateCurrentPageIfNeeded ( basedOn: contentOffset)
184
179
}
185
180
}
0 commit comments