/
CollectionView.swift
56 lines (42 loc) · 1.88 KB
/
CollectionView.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
import Foundation
import UIKit
import Observable
class CollectionView: UICollectionView {
@MutableObservable private var sPercentage:Float = 0
var scrollPercentage: Observable<Float> {
var scrollPercentage: ImmutableObservable<Float> {
return _sPercentage
}
convenience init() {
let size = (UIScreen.main.bounds.width / 2) - 30
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: size, height: size)
layout.minimumLineSpacing = 10
layout.minimumInteritemSpacing = 10
layout.scrollDirection = .horizontal
self.init(frame: .zero, collectionViewLayout: layout)
register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CellReuseIdentifier")
translatesAutoresizingMaskIntoConstraints = false
dataSource = self
delegate = self
contentInset = UIEdgeInsets(top: 30, left: 0, bottom: 40, right: 0)
backgroundColor = nil
clipsToBounds = false
showsHorizontalScrollIndicator = false
}
}
extension CollectionView: UICollectionViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
sPercentage = Float((scrollView.contentOffset.x / frame.width) / ((contentSize.width - frame.width) / frame.width))
}
}
extension CollectionView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellReuseIdentifier", for: indexPath)
cell.backgroundColor = UIColor(red: 95/255, green: 20/255, blue: 255/255, alpha: 1)
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}
}