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
163 lines (130 loc) · 6.27 KB
/
CollectionViewPagingLayout.swift
File metadata and controls
163 lines (130 loc) · 6.27 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
//
// CollectionViewPagingLayout.swift
// CollectionViewPagingLayout
//
// Created by Amir Khorsandi on 12/23/19.
// Copyright © 2019 Amir Khorsandi. All rights reserved.
//
import UIKit
public protocol CollectionViewPagingLayoutDelegate: class {
func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int)
}
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 {
guard let collectionView = collectionView else {
return .zero
}
return CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
}
private var numberOfItems: Int {
collectionView?.numberOfItems(inSection: 0) ?? 0
}
// 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)
}
// MARK: UICollectionViewLayout
override public func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
true
}
override public func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let currentScrollOffset = self.currentScrollOffset
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: [UICollectionViewLayoutAttributes] = []
for index in startIndex..<endIndex {
let cellAttributes = UICollectionViewLayoutAttributes(forCellWith: IndexPath(row: index, section: 0))
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(cellAttributes)
}
return attributesArray
}
override public func invalidateLayout() {
super.invalidateLayout()
updateCurrentPageIfNeeded()
}
// MARK: Private functions
private func updateCurrentPageIfNeeded() {
var currentPage: Int = 0
if let collectionView = collectionView {
let pageSize = scrollDirection == .horizontal ? collectionView.frame.width : collectionView.frame.height
let contentOffset = scrollDirection == .horizontal ? (collectionView.contentOffset.x + collectionView.contentInset.left) : (collectionView.contentOffset.y + collectionView.contentInset.top)
currentPage = Int(round(contentOffset / 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)
collectionView?.setContentOffset(contentOffset, animated: animated)
}
}