forked from amirdew/CollectionViewPagingLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionViewPagingLayout.swift
More file actions
264 lines (219 loc) · 10.8 KB
/
CollectionViewPagingLayout.swift
File metadata and controls
264 lines (219 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
// CollectionViewPagingLayout.swift
// CollectionViewPagingLayout
//
// Created by Amir Khorsandi on 12/23/19.
// Copyright © 2019 Amir Khorsandi. All rights reserved.
//
import UIKit
public protocol CollectionViewPagingLayoutDelegate: class {
/// Calls when the current page changes
///
/// - Parameter layout: a reference to the layout class
/// - Parameter currentPage: the new current page index
func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int)
/// Calls when the user taps on the `TransformableView.selectableView`
/// to enable this functionality you need to call `configureTapOnCollectionView()` after setting the layout
///
/// - Parameter layout: a reference to the layout class
/// - Parameter indexPath: IndexPath for the selected cell
func collectionViewPagingLayout(_ layout: CollectionViewPagingLayout, didSelectItemAt indexPath: IndexPath)
}
public extension CollectionViewPagingLayoutDelegate {
func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int) {}
func collectionViewPagingLayout(_ layout: CollectionViewPagingLayout, didSelectItemAt indexPath: IndexPath) {}
}
public class CollectionViewPagingLayout: UICollectionViewLayout {
// MARK: Properties
public var numberOfVisibleItems: Int?
public var scrollDirection: UICollectionView.ScrollDirection = .horizontal
public weak var delegate: CollectionViewPagingLayoutDelegate?
override public var collectionViewContentSize: CGSize {
getContentSize()
}
public private(set) var currentPage: Int = 0 {
didSet {
delegate?.onCurrentPageChanged(layout: self, currentPage: currentPage)
}
}
private var currentScrollOffset: CGFloat {
let visibleRect = self.visibleRect
return scrollDirection == .horizontal ? (visibleRect.minX / max(visibleRect.width, 1)) : (visibleRect.minY / max(visibleRect.height, 1))
}
private var visibleRect: CGRect {
collectionView.map { CGRect(origin: $0.contentOffset, size: $0.bounds.size) } ?? .zero
}
private var numberOfItems: Int {
guard let numberOfSections = collectionView?.numberOfSections, numberOfSections > 0 else {
return 0
}
return (0..<numberOfSections)
.compactMap { collectionView?.numberOfItems(inSection: $0) }
.reduce(0, +)
}
private var currentPageCache: Int?
private var attributesCache: [(page: Int, attributes:UICollectionViewLayoutAttributes)]?
private var scrollToSelectedCell: Bool = false
// MARK: Public functions
public func setCurrentPage(_ page: Int, animated: Bool = true) {
safelySetCurrentPage(page, animated: animated)
}
public func goToNextPage(animated: Bool = true) {
setCurrentPage(currentPage + 1, animated: animated)
}
public func goToPreviousPage(animated: Bool = true) {
setCurrentPage(currentPage - 1, animated: animated)
}
public func configureTapOnCollectionView(goToSelectedPage: Bool = false) {
self.scrollToSelectedCell = goToSelectedPage
addTapGestureToCollectionView()
}
// MARK: UICollectionViewLayout
override public func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
if newBounds.size != visibleRect.size {
currentPageCache = currentPage
}
return true
}
override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let currentScrollOffset = self.currentScrollOffset
let numberOfItems = self.numberOfItems
let attributesCount = numberOfVisibleItems ?? numberOfItems
let visibleRangeMid = attributesCount / 2
let currentPageIndex = Int(round(currentScrollOffset))
var initialStartIndex = currentPageIndex - visibleRangeMid
var initialEndIndex = currentPageIndex + visibleRangeMid
if attributesCount % 2 != 0 {
if currentPageIndex < visibleRangeMid {
initialStartIndex -= 1
} else {
initialEndIndex += 1
}
}
let startIndexOutOfBounds = max(0, -initialStartIndex)
let endIndexOutOfBounds = max(0, initialEndIndex - numberOfItems)
let startIndex = max(0, initialStartIndex - endIndexOutOfBounds)
let endIndex = min(numberOfItems, initialEndIndex + startIndexOutOfBounds)
var attributesArray: [(page: Int, attributes:UICollectionViewLayoutAttributes)] = []
var section = 0
var numberOfItemsInSection = collectionView?.numberOfItems(inSection: section) ?? 0
var numberOfItemsInPrevSections = 0
for index in startIndex..<endIndex {
var item = index - numberOfItemsInPrevSections
while item >= numberOfItemsInSection {
numberOfItemsInPrevSections += numberOfItemsInSection
section += 1
numberOfItemsInSection = collectionView?.numberOfItems(inSection: section) ?? 0
item = index - numberOfItemsInPrevSections
}
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(item: item, section: section))
let pageIndex = CGFloat(index)
let progress = pageIndex - currentScrollOffset
var zIndex = Int(-abs(round(progress)))
let cell = collectionView?.cellForItem(at: cellAttributes.indexPath)
if let cell = cell as? TransformableView {
cell.transform(progress: progress)
zIndex = cell.zPosition(progress: progress)
}
if cell == nil || cell is TransformableView {
cellAttributes.frame = visibleRect
} else {
cellAttributes.frame = CGRect(origin: CGPoint(x: pageIndex * visibleRect.width, y: 0), size: visibleRect.size)
}
cellAttributes.zIndex = zIndex
attributesArray.append((page: Int(pageIndex), attributes: cellAttributes))
}
attributesCache = attributesArray
return attributesArray.map(\.attributes)
}
override public func invalidateLayout() {
super.invalidateLayout()
if let page = currentPageCache {
setCurrentPage(page, animated: false)
currentPageCache = nil
} else {
updateCurrentPageIfNeeded()
}
}
// MARK: Private functions
private func updateCurrentPageIfNeeded() {
var currentPage: Int = 0
if let collectionView = collectionView {
let contentOffset = collectionView.contentOffset
let pageSize = scrollDirection == .horizontal ? collectionView.frame.width : collectionView.frame.height
let offset = scrollDirection == .horizontal ?
(contentOffset.x + collectionView.contentInset.left) :
(contentOffset.y + collectionView.contentInset.top)
if pageSize > 0 {
currentPage = Int(round(offset / pageSize))
}
}
if currentPage != self.currentPage {
self.currentPage = currentPage
}
}
private func getContentSize() -> CGSize {
var safeAreaLeftRight: CGFloat = 0
var safeAreaTopBottom: CGFloat = 0
if #available(iOS 11, *) {
safeAreaLeftRight = (collectionView?.safeAreaInsets.left ?? 0) + (collectionView?.safeAreaInsets.right ?? 0)
safeAreaTopBottom = (collectionView?.safeAreaInsets.top ?? 0) + (collectionView?.safeAreaInsets.bottom ?? 0)
}
if scrollDirection == .horizontal {
return CGSize(width: CGFloat(numberOfItems) * visibleRect.width, height: visibleRect.height - safeAreaTopBottom)
} else {
return CGSize(width: visibleRect.width - safeAreaLeftRight, height: CGFloat(numberOfItems) * visibleRect.height)
}
}
private func safelySetCurrentPage(_ page: Int, animated: Bool) {
let pageSize = scrollDirection == .horizontal ? visibleRect.width : visibleRect.height
let contentSize = scrollDirection == .horizontal ? collectionViewContentSize.width : collectionViewContentSize.height
let maxPossibleOffset = contentSize - pageSize
var offset = pageSize * CGFloat(page)
offset = max(0, offset)
offset = min(offset, maxPossibleOffset)
let contentOffset: CGPoint = scrollDirection == .horizontal ? CGPoint(x: offset, y: 0) : CGPoint(x: 0, y: offset)
CATransaction.begin()
CATransaction.setCompletionBlock { [weak self] in
self?.invalidateLayout()
}
collectionView?.setContentOffset(contentOffset, animated: animated)
CATransaction.commit()
// this is necessary when we want to set the current page without animation
if !animated, page != currentPage, let collectionView = collectionView {
collectionView.performBatchUpdates({
collectionView.collectionViewLayout.invalidateLayout()
})
}
}
private func addTapGestureToCollectionView() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(tapOnCollectionView(gesture:)))
collectionView?.addGestureRecognizer(gesture)
}
@objc private func tapOnCollectionView(gesture: UITapGestureRecognizer) {
var items = collectionView?.visibleCells.compactMap { cell -> (cell: UICollectionViewCell, rect: CGRect, attributes: UICollectionViewLayoutAttributes, page: Int)? in
guard let indexPath = collectionView?.indexPath(for: cell),
let view = cell as? TransformableView,
let selectableView = view.selectableView,
let attributesAndPage = attributesCache?.first(where: { $0.attributes.indexPath == indexPath }) else {
return nil
}
let rect = selectableView.superview?.convert(selectableView.frame, to: collectionView) ?? .zero
return (cell: cell, rect: rect, attributes: attributesAndPage.attributes, page: attributesAndPage.page)
} ?? []
items.sort { $0.attributes.zIndex > $1.attributes.zIndex }
let location = gesture.location(in: gesture.view)
var findSelected = false
for item in items {
if !findSelected, item.rect.contains(location) {
delegate?.collectionViewPagingLayout(self, didSelectItemAt: item.attributes.indexPath)
item.cell.isSelected = true
findSelected = true
if scrollToSelectedCell {
setCurrentPage(item.page, animated: true)
}
}
item.cell.isSelected = false
}
}
}