-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathViewController.swift
More file actions
179 lines (147 loc) · 5.99 KB
/
Copy pathViewController.swift
File metadata and controls
179 lines (147 loc) · 5.99 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
//
// ViewController.swift
// Example
//
// Created by Anton Malygin on 17.02.2021.
//
import StableCollectionViewLayout
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
var rowsPerSection: Int = 1
var scrollDirection: UICollectionView.ScrollDirection = .vertical
private lazy var layout: StableCollectionViewFlowLayout = {
let layout = StableCollectionViewFlowLayout()
layout.scrollDirection = scrollDirection
layout.minimumInteritemSpacing = 1
return layout
}()
private lazy var collectionView = UICollectionView(
frame: .zero,
collectionViewLayout: layout
)
var items: [(String, CGFloat)] = (0 ..< 100).map { index in ("\(index)", randomHeight()) }
let batchCount = 30
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
view.backgroundColor = .white
}
private func setupCollectionView() {
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
if #available(iOS 13.0, *) {
collectionView.backgroundColor = .systemBackground
} else {
collectionView.backgroundColor = .white
}
collectionView.translatesAutoresizingMaskIntoConstraints = false
let top = collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
let bottom = collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
let leading = collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor)
let trailing = collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
NSLayoutConstraint.activate([top, bottom, trailing, leading])
collectionView.registerCell(of: DemoCollectionViewCell.self)
}
// MARK: - UICollectionViewDataSource
func numberOfSections(in _: UICollectionView) -> Int {
return 1
}
func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
return items.count
}
func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
let item = items[indexPath.row]
let cell = collectionView.dequeueReusableCell(DemoCollectionViewCell.self, for: indexPath)
cell.textLabel.text = item.0
return cell
}
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
return .zero
}
let item = items[indexPath.row]
if flowLayout.scrollDirection == .vertical {
return CGSize(width: collectionView.frame.width / CGFloat(rowsPerSection) - 10, height: item.1)
} else {
let height = collectionView.frame.height / CGFloat(rowsPerSection)
- flowLayout.minimumLineSpacing * CGFloat(rowsPerSection) * 2
return CGSize(
width: item.1,
height: height
)
}
}
private static func randomHeight() -> CGFloat {
CGFloat(floor(Double.random(in: 100 ... 200)))
}
private static func randomBool() -> Bool {
return Bool.random()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}
let inset = CGFloat(100)
if layout.scrollDirection == .vertical {
if scrollView.contentOffset.y < inset {
moveUp()
} else if scrollView.contentOffset.y + scrollView.frame.height > scrollView.contentSize.height - inset {
moveDown()
}
} else {
if scrollView.contentOffset.x < inset {
moveUp()
} else if scrollView.contentOffset.x + scrollView.frame.width > scrollView.contentSize.width - inset {
moveDown()
}
}
}
private func moveUp() {
let tail = items.suffix(batchCount)
let indexPaths = (0 ..< batchCount).map { IndexPath(row: $0, section: 0) }
let deleteIndexPaths = (items.count - batchCount ..< items.count).map { IndexPath(row: $0, section: 0) }
items.removeLast(batchCount)
items.insert(contentsOf: tail, at: 0)
UIView.performWithoutAnimation {
self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: deleteIndexPaths)
self.collectionView.insertItems(at: indexPaths)
}, completion: nil)
}
}
private func moveDown() {
let head = items.prefix(batchCount)
let indexPaths = (items.count - batchCount ..< items.count).map { IndexPath(row: $0, section: 0) }
let deleteIndexPaths = (0 ..< batchCount).map { IndexPath(row: $0, section: 0) }
items.removeFirst(batchCount)
items.append(contentsOf: head)
UIView.performWithoutAnimation {
self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: deleteIndexPaths)
self.collectionView.insertItems(at: indexPaths)
}, completion: nil)
}
}
}
extension UICollectionView {
func registerCell<T: UICollectionViewCell>(of type: T.Type) {
register(type, forCellWithReuseIdentifier: NSStringFromClass(type))
}
func dequeueReusableCell<T: UICollectionViewCell>(
_ type: T.Type,
for indexPath: IndexPath
) -> T {
guard let cell = dequeueReusableCell(withReuseIdentifier: NSStringFromClass(type), for: indexPath) as? T else {
fatalError("No cell for given type registered, did you do it via: `registerCell`")
}
return cell
}
}