-
Notifications
You must be signed in to change notification settings - Fork 184
/
CollectionViewLayout.swift
111 lines (81 loc) · 3.33 KB
/
CollectionViewLayout.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
//
// RAMCollectionViewLayout.swift
// RAMReel
//
// Created by Mikhail Stepkin on 4/9/15.
// Copyright (c) 2015 Ramotion. All rights reserved.
//
import UIKit
/**
Example collection view layout
*/
@objc(RAMCollectionViewLayout)
class RAMCollectionViewLayout: UICollectionViewFlowLayout {
internal override func prepare() {
super.prepare()
updateInsets()
}
func updateInsets() {
if let collectionView = self.collectionView {
let insets = (collectionView.bounds.height - itemHeight)/2
collectionView.contentInset = UIEdgeInsets(top: insets, left: 0, bottom: insets, right: 0)
}
}
internal override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return self.layoutAttributesForItem(at: itemIndexPath)
}
internal override func layoutAttributesForItem(at indexPath:IndexPath) -> UICollectionViewLayoutAttributes {
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
self.modifyLayoutAttributes(attributes)
return attributes
}
internal override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var allAttributesInRect = [(UICollectionViewLayoutAttributes, CGFloat)]()
if let numberOfItems = collectionView?.numberOfItems(inSection: 0) {
for item in 0 ..< numberOfItems {
let indexPath = IndexPath(item: item, section: 0)
let attributes = self.layoutAttributesForItem(at: indexPath)
if rect.intersects(attributes.frame) {
let intersection = rect.intersection(attributes.frame)
allAttributesInRect.append((attributes, intersection.area))
}
}
}
allAttributesInRect.sort {
let (_, a1) = $0
let (_, a2) = $1
return a1 > a2
}
let attributes = allAttributesInRect.map ({ (attr, _) in
attr
})
return attributes
}
var itemHeight: CGFloat = 44
func modifyLayoutAttributes(_ layoutattributes: UICollectionViewLayoutAttributes) {
if
let collectionView = self.collectionView
{
var frame = layoutattributes.frame
frame.size.height = itemHeight
frame.size.width = collectionView.bounds.width
frame.origin.x = collectionView.bounds.origin.x
frame.origin.y = itemHeight * CGFloat((layoutattributes.indexPath as NSIndexPath).item)
layoutattributes.frame = frame
}
}
internal override var collectionViewContentSize : CGSize {
guard let collectionView = self.collectionView else {
return CGSize.zero
}
let number = collectionView.numberOfItems(inSection: 0)
let height = CGFloat(number) * itemHeight
let size = CGSize(width: collectionView.bounds.width, height: height)
return size
}
}
private extension CGRect {
var area: CGFloat {
return self.height * self.width
}
}