-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduleCollectionViewLayout.swift
285 lines (217 loc) · 11.9 KB
/
ScheduleCollectionViewLayout.swift
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// ScheduleCollectionViewLayout.swift
// CyxbsMobile2019_iOS
//
// Created by SSR on 2023/9/11.
// Copyright © 2023 Redrock. All rights reserved.
//
/*
---------------------------
wake up | -1 < x < 1
---------------------------
1 | 1 <= x < 2
---------------------------
1 - 2 | -2 < x <= -1
---------------------------
2 | 2 <= x < 3
---------------------------
*/
import UIKit
// MARK: ScheduleCollectionViewLayoutDataSource
@objc public
protocol ScheduleCollectionViewLayoutDataSource {
func collectionView(_ collectionView: UICollectionView, layout: ScheduleCollectionViewLayout, columnOfItemAtIndexPath indexPath: IndexPath) -> Int
func collectionView(_ collectionView: UICollectionView, layout: ScheduleCollectionViewLayout, lineOfItemAtIndexPath indexPath: IndexPath) -> Int
func collectionView(_ collectionView: UICollectionView, layout: ScheduleCollectionViewLayout, lenthOfItemAtIndexPath indexPath: IndexPath) -> Int
func collectionView(_ collectionView: UICollectionView, layout: ScheduleCollectionViewLayout, numberOfSupplementaryOfKind kind: String, inSection section: Int) -> Int
func collectionView(_ collectionView: UICollectionView, layout: ScheduleCollectionViewLayout, persentOfPointAtIndexPath indexPath: IndexPath) -> CGFloat
}
// MARK: UICollectionView.ElementKindSection
extension UICollectionView {
public enum ElementKindSection: String {
case header = "Redrock.UICollectionView.ElementKindSection.header"
case leading = "Redrock.UICollectionView.ElementKindSection.leading"
case placeHolder = "Redrock.UICollectionView.ElementKindSection.placeHolder"
case pointHolder = "Redrock.UICollectionView.ElementKindSection.pointHolder"
}
public func register(_ viewClass: AnyClass?, forElementKindSection elementKind: ElementKindSection, withReuseIdentifier identifier: String) {
register(viewClass, forSupplementaryViewOfKind: elementKind.rawValue, withReuseIdentifier: identifier)
if let layout = collectionViewLayout as? ScheduleCollectionViewLayout {
layout.register(viewClass, forSupplementaryViewOfKind: elementKind.rawValue)
}
}
}
extension UICollectionView {
var ry_layout: ScheduleCollectionViewLayout? {
collectionViewLayout as? ScheduleCollectionViewLayout
}
}
// MARK: ScheduleCollectionViewLayout
open class ScheduleCollectionViewLayout: UICollectionViewLayout {
// MARK: Property
open weak var dataSource: ScheduleCollectionViewLayoutDataSource?
open var lineSpacing: CGFloat = 2
open var columnSpacing: CGFloat = 2
open var widthForLeadingSupplementaryView: CGFloat = 30
private(set) var heightForHeaderSupplementaryView: CGFloat = 0
open var aspectRatio: CGFloat = 46.0 / 50.0
open var heightForHeader: CGFloat = 50
open var pageCalculation: Int = 0
private(set) open var itemSize: CGSize = .zero
open var pageShows: Int = 1 {
didSet {
if pageShows < 1 {
pageShows = 1
}
}
}
// MARK: Fast Use
open var ry_collectionView: UICollectionView {
collectionView!
}
// MARK: Private
private var itemAttributes: [IndexPath: UICollectionViewLayoutAttributes] = [:]
private var supplementaryAttributes: [String: [IndexPath: UICollectionViewLayoutAttributes]] = [:]
// MARK: Method - middle
open func register(_ viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String) {
if supplementaryAttributes[elementKind] == nil {
supplementaryAttributes[elementKind] = [:]
}
}
// MARK: Method - layoutAttributes
open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let numberOfSections = collectionView?.dataSource?.numberOfSections?(in: ry_collectionView) ?? 0
if numberOfSections <= 0 { return nil }
var result = [UICollectionViewLayoutAttributes]()
for section in 0 ..< numberOfSections {
for elementKind in supplementaryAttributes.keys {
let numberOfSupplementaryOfKind = dataSource?.collectionView(ry_collectionView, layout: self, numberOfSupplementaryOfKind: elementKind, inSection: section) ?? 0
if numberOfSupplementaryOfKind <= 0 { continue }
for item in 0..<numberOfSupplementaryOfKind {
let indexPath = IndexPath(item: item, section: section)
if let attributes = layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath) {
if rect.intersects(attributes.frame) {
result.append(attributes)
}
}
}
}
let itemCount = collectionView?.dataSource?.collectionView(ry_collectionView, numberOfItemsInSection: section) ?? 0
if itemCount <= 0 { continue }
for item in 0 ..< itemCount {
let indexPath = IndexPath(item: item, section: section)
if let attributes = layoutAttributesForItem(at: indexPath) {
if rect.intersects(attributes.frame) {
result.append(attributes)
}
}
}
}
return result
}
open override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let dataSource else { return nil }
let columnOfItem = dataSource.collectionView(ry_collectionView, layout: self, columnOfItemAtIndexPath: indexPath)
let lineOfItem = dataSource.collectionView(ry_collectionView, layout: self, lineOfItemAtIndexPath: indexPath)
let lenthOfItem = dataSource.collectionView(ry_collectionView, layout: self, lenthOfItemAtIndexPath: indexPath)
let attribute = itemAttributes[indexPath] ?? UICollectionViewLayoutAttributes(forCellWith: indexPath)
attribute.frame = CGRect(
x: CGFloat(indexPath.section) * ry_collectionView.bounds.width + widthForLeadingSupplementaryView + CGFloat(columnOfItem - 1) * (itemSize.width + columnSpacing),
y: heightForHeaderSupplementaryView + CGFloat(lineOfItem - 1) * (itemSize.height + lineSpacing) + lineSpacing,
width: itemSize.width,
height: CGFloat(lenthOfItem) * itemSize.height + CGFloat(lenthOfItem - 1) * columnSpacing)
itemAttributes[indexPath] = attribute
return attribute
}
open override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let elementKindSection = UICollectionView.ElementKindSection(rawValue: elementKind) else { return nil }
guard let elementKindAttributes = supplementaryAttributes[elementKind] else { return nil }
let attributes = elementKindAttributes[indexPath] ?? UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: indexPath)
setupSupplementaryAttributes(attributes, elementKindSection: elementKindSection)
supplementaryAttributes[elementKind]?[indexPath] = attributes
return attributes
}
func setupSupplementaryAttributes(_ attributes :UICollectionViewLayoutAttributes, elementKindSection: UICollectionView.ElementKindSection) {
let indexPath = attributes.indexPath
let pageWidth = ry_collectionView.bounds.width / CGFloat(pageShows)
switch elementKindSection {
case .header:
attributes.zIndex = 10
if indexPath.item == 0 {
attributes.frame = CGRect(
x: CGFloat(indexPath.section) * pageWidth,
y: ry_collectionView.contentOffset.y,
width: widthForLeadingSupplementaryView,
height: heightForHeaderSupplementaryView)
} else {
attributes.frame = CGRect(
x: CGFloat(indexPath.section) * ry_collectionView.width + widthForLeadingSupplementaryView + CGFloat(indexPath.item - 1) * (columnSpacing + itemSize.width),
y: ry_collectionView.contentOffset.y,
width: itemSize.width,
height: heightForHeaderSupplementaryView)
}
case .leading:
attributes.frame = CGRect(
x: CGFloat(indexPath.section) * ry_collectionView.width,
y: heightForHeaderSupplementaryView + CGFloat(indexPath.item) * (lineSpacing + itemSize.height),
width: widthForLeadingSupplementaryView,
height: itemSize.height)
case .placeHolder:
attributes.frame = CGRect(
x: CGFloat(indexPath.section) * pageWidth + widthForLeadingSupplementaryView,
y: ry_collectionView.contentOffset.y, // + heightForHeaderSupplementaryView
width: pageWidth - widthForLeadingSupplementaryView,
height: ry_collectionView.bounds.height - heightForHeaderSupplementaryView)
case .pointHolder:
let persent = dataSource?.collectionView(ry_collectionView, layout: self, persentOfPointAtIndexPath: indexPath) ?? 0
print("todo")
}
}
// MARK: others
open override func prepare() {
calculateLayoutIfNeeded()
}
open override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
true
}
open override func invalidateLayout(with context: UICollectionViewLayoutInvalidationContext) {
if context.invalidateDataSourceCounts {
itemAttributes.removeAll(keepingCapacity: true)
for key in supplementaryAttributes {
supplementaryAttributes[key.key]?.removeAll(keepingCapacity: true)
}
}
supplementaryAttributes[UICollectionView.ElementKindSection.header.rawValue]?.forEach { entry in
self.setupSupplementaryAttributes(entry.value, elementKindSection: .header)
}
}
open override var collectionViewContentSize: CGSize {
let itemCount = 13
let sections = collectionView?.dataSource?.numberOfSections?(in: ry_collectionView) ?? 0
return CGSize(
width: CGFloat(sections) * ry_collectionView.bounds.width,
height: heightForHeaderSupplementaryView + CGFloat(itemCount) * (itemSize.height + lineSpacing))
}
open override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let toTime = Int(ry_collectionView.contentOffset.y / (itemSize.height + lineSpacing) + 0.5)
let toY = (itemSize.height + lineSpacing) * CGFloat(toTime)
var index = Int(proposedContentOffset.x / ry_collectionView.bounds.width + 0.5)
let remainder = proposedContentOffset.x - CGFloat(index) * ry_collectionView.bounds.width
if velocity.x > 0.6 || (velocity.x > 0.3 && remainder > ry_collectionView.bounds.width / 3) {
index += 1
}
if velocity.x < -0.6 || (velocity.x < -0.3 && remainder < ry_collectionView.bounds.width / 3) {
index -= 1
}
index = max(index, pageCalculation - 1)
index = min(index, pageCalculation + 1)
let toX = ry_collectionView.bounds.width * CGFloat(index)
return CGPoint(x: toX, y: toY)
}
// MARK: private
private func calculateLayoutIfNeeded() {
let width = (ry_collectionView.bounds.width - widthForLeadingSupplementaryView) / 7 - columnSpacing
itemSize = CGSize(width: width, height: width / aspectRatio)
heightForHeaderSupplementaryView = width / aspectRatio
}
}