-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathCardsCollectionViewLayout.swift
148 lines (118 loc) · 5.31 KB
/
CardsCollectionViewLayout.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
//
// CardsCollectionViewLayout.swift
// CardsExample
//
// Created by Filipp Fediakov on 18.08.17.
// Copyright © 2017 filletofish. All rights reserved.
//
import UIKit
open class CardsCollectionViewLayout: UICollectionViewLayout {
// MARK: - Layout configuration
public var itemSize: CGSize = CGSize(width: 200, height: 300) {
didSet{
invalidateLayout()
}
}
public var spacing: CGFloat = 10.0 {
didSet{
invalidateLayout()
}
}
public var maximumVisibleItems: Int = 4 {
didSet{
invalidateLayout()
}
}
// MARK: UICollectionViewLayout
override open var collectionView: UICollectionView {
return super.collectionView!
}
override open var collectionViewContentSize: CGSize {
let itemsCount = CGFloat(collectionView.numberOfItems(inSection: 0))
return CGSize(width: collectionView.bounds.width * itemsCount,
height: collectionView.bounds.height)
}
override open func prepare() {
super.prepare()
assert(collectionView.numberOfSections == 1, "Multiple sections aren't supported!")
}
override open func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let totalItemsCount = collectionView.numberOfItems(inSection: 0)
let minVisibleIndex = max(Int(collectionView.contentOffset.x) / Int(collectionView.bounds.width), 0)
let maxVisibleIndex = min(minVisibleIndex + maximumVisibleItems, totalItemsCount)
let contentCenterX = collectionView.contentOffset.x + (collectionView.bounds.width / 2.0)
let deltaOffset = Int(collectionView.contentOffset.x) % Int(collectionView.bounds.width)
let percentageDeltaOffset = CGFloat(deltaOffset) / collectionView.bounds.width
let visibleIndices = stride(from: minVisibleIndex, to: maxVisibleIndex, by: 1)
let attributes: [UICollectionViewLayoutAttributes] = visibleIndices.map { index in
let indexPath = IndexPath(item: index, section: 0)
return computeLayoutAttributesForItem(indexPath: indexPath,
minVisibleIndex: minVisibleIndex,
contentCenterX: contentCenterX,
deltaOffset: CGFloat(deltaOffset),
percentageDeltaOffset: percentageDeltaOffset)
}
return attributes
}
override open func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let contentCenterX = collectionView.contentOffset.x + (collectionView.bounds.width / 2.0)
let minVisibleIndex = Int(collectionView.contentOffset.x) / Int(collectionView.bounds.width)
let deltaOffset = Int(collectionView.contentOffset.x) % Int(collectionView.bounds.width)
let percentageDeltaOffset = CGFloat(deltaOffset) / collectionView.bounds.width
return computeLayoutAttributesForItem(indexPath: indexPath,
minVisibleIndex: minVisibleIndex,
contentCenterX: contentCenterX,
deltaOffset: CGFloat(deltaOffset),
percentageDeltaOffset: percentageDeltaOffset)
}
override open func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
// MARK: - Layout computations
fileprivate extension CardsCollectionViewLayout {
private func scale(at index: Int) -> CGFloat {
let translatedCoefficient = CGFloat(index) - CGFloat(self.maximumVisibleItems) / 2
return CGFloat(pow(0.95, translatedCoefficient))
}
private func transform(atCurrentVisibleIndex visibleIndex: Int, percentageOffset: CGFloat) -> CGAffineTransform {
var rawScale = visibleIndex < maximumVisibleItems ? scale(at: visibleIndex) : 1.0
if visibleIndex != 0 {
let previousScale = scale(at: visibleIndex - 1)
let delta = (previousScale - rawScale) * percentageOffset
rawScale += delta
}
return CGAffineTransform(scaleX: rawScale, y: rawScale)
}
fileprivate func computeLayoutAttributesForItem(indexPath: IndexPath,
minVisibleIndex: Int,
contentCenterX: CGFloat,
deltaOffset: CGFloat,
percentageDeltaOffset: CGFloat) -> UICollectionViewLayoutAttributes {
let attributes = UICollectionViewLayoutAttributes(forCellWith:indexPath)
let visibleIndex = indexPath.row - minVisibleIndex
attributes.size = itemSize
let midY = self.collectionView.bounds.midY
attributes.center = CGPoint(x: contentCenterX + spacing * CGFloat(visibleIndex),
y: midY + spacing * CGFloat(visibleIndex))
attributes.zIndex = maximumVisibleItems - visibleIndex
attributes.transform = transform(atCurrentVisibleIndex: visibleIndex,
percentageOffset: percentageDeltaOffset)
switch visibleIndex {
case 0:
attributes.center.x -= deltaOffset
break
case 1..<maximumVisibleItems:
attributes.center.x -= spacing * percentageDeltaOffset
attributes.center.y -= spacing * percentageDeltaOffset
if visibleIndex == maximumVisibleItems - 1 {
attributes.alpha = percentageDeltaOffset
}
break
default:
attributes.alpha = 0
break
}
return attributes
}
}