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

Commit 009cd9a

Browse files
authored
Merge pull request #2 from amirdew/master
merge newest code
2 parents 1ecba9d + 62a5003 commit 009cd9a

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

CollectionViewPagingLayout.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = "CollectionViewPagingLayout"
4-
s.version = "0.1.3"
4+
s.version = "0.1.5"
55
s.summary = "Simple layout for making paging effects with UICollectionView."
66

77
s.description = <<-DESC

Lib/CollectionViewPagingLayout.swift

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
4343
}
4444

4545
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, +)
5349
}
5450

5551

@@ -76,6 +72,7 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
7672

7773
override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
7874
let currentScrollOffset = self.currentScrollOffset
75+
let numberOfItems = self.numberOfItems
7976
let attributesCount = numberOfVisibleItems ?? numberOfItems
8077
let visibleRangeMid = attributesCount / 2
8178
let currentPageIndex = Int(round(currentScrollOffset))
@@ -94,23 +91,19 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
9491
let endIndex = min(numberOfItems, initialEndIndex + startIndexOutOfBounds)
9592

9693
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
9897
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
111104
}
112-
113-
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(row: row, section: section))
105+
106+
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: item, section: section))
114107
let pageIndex = CGFloat(index)
115108
let progress = pageIndex - currentScrollOffset
116109
var zIndex = Int(-abs(round(progress)))
@@ -142,15 +135,16 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
142135

143136
// MARK: Private functions
144137

145-
private func updateCurrentPageIfNeeded() {
138+
private func updateCurrentPageIfNeeded(basedOn contentOffset: CGPoint? = nil) {
146139
var currentPage: Int = 0
147140
if let collectionView = collectionView {
141+
let contentOffset = contentOffset ?? collectionView.contentOffset
148142
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)
152146
if pageSize > 0 {
153-
currentPage = Int(round(contentOffset / pageSize))
147+
currentPage = Int(round(offset / pageSize))
154148
}
155149
}
156150
if currentPage != self.currentPage {
@@ -181,5 +175,6 @@ public class CollectionViewPagingLayout: UICollectionViewLayout {
181175
offset = min(offset, maxPossibleOffset)
182176
let contentOffset: CGPoint = scrollDirection == .horizontal ? CGPoint(x: offset, y: 0) : CGPoint(x: 0, y: offset)
183177
collectionView?.setContentOffset(contentOffset, animated: animated)
178+
updateCurrentPageIfNeeded(basedOn: contentOffset)
184179
}
185180
}

0 commit comments

Comments
 (0)