-
Notifications
You must be signed in to change notification settings - Fork 0
/
PassbookLayout.swift
executable file
·193 lines (155 loc) · 7.48 KB
/
PassbookLayout.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
//
// PassbookLayout.swift
// PassbookLayout
//
// Created by 田程元 on 15/3/23.
// Copyright (c) 2015年 田程元. All rights reserved.
//
import UIKit
struct PassMetrics {
/// Size of a state of a pass
var size:CGSize
/// Amount of "pixels" of overlap between this pass and others.
var overlap:CGFloat
}
struct PassbookLayoutMetrics{
/// Normal is the real size of the pass, the "full screen" display of it.
var normal:PassMetrics
/// Collapsed is when
var collapsed:PassMetrics
/// The size of the bottom stack when a pass is selected and all others are stacked at bottom
var bottomStackedTotalHeight:CGFloat
/// The visible size of each cell in the bottom stack
var bottomStackedHeight:CGFloat
}
struct PassbookLayoutEffects{
/// How much of the pulling is translated into movement on the top. An inheritance of 0 disables this feature (same as bouncesTop)
var inheritance:CGFloat
/// Allows for bouncing when reaching the top
var bouncesTop:Bool
/// Allows the cells get "stuck" on the top, instead of just scrolling outside
var sticksTop:Bool
}
class PassbookLayout: UICollectionViewLayout {
var metrics:PassbookLayoutMetrics = PassbookLayoutMetrics(normal: PassMetrics(size: CGSizeMake(320.0, 420.0), overlap: 0.0), collapsed: PassMetrics(size: CGSizeMake(320.0, 96.0), overlap: 15.0), bottomStackedTotalHeight: 32.00, bottomStackedHeight: 8.0)
var effects:PassbookLayoutEffects = PassbookLayoutEffects(inheritance: 0.20, bouncesTop: true, sticksTop: true)
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
let attributes:UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
let selectedIndexPaths = (self.collectionView?.indexPathsForSelectedItems())! as NSArray
if (selectedIndexPaths.count>0 && selectedIndexPaths[0] as! NSIndexPath == indexPath)
{
// Layout selected cell (normal size)
attributes.frame = frameForSelectedPass((collectionView?.bounds)!, m: metrics)
}else if (selectedIndexPaths.count>0){
// Layout unselected cell (bottom-stuck)
attributes.frame = frameForUnselectedPass(indexPath, indexPathSelected:selectedIndexPaths[0] as! NSIndexPath,b:(collectionView?.bounds)!, m:metrics)
}
else{
// Layout collapsed cells (collapsed size)
let isLast:Bool = (indexPath.item == (collectionView?.numberOfItemsInSection(indexPath.section))!-1)
attributes.frame = frameForPassAtIndex(indexPath, isLastCell: isLast, b: (collectionView?.bounds)!, m: metrics, e: effects)
}
attributes.zIndex = zIndexForPassAtIndex(indexPath)
return attributes
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let range = rangeForVisibleCells(rect, count: (collectionView?.numberOfItemsInSection(0))! , m: metrics)
// Uncomment to see the current range
//NSLog(@"Visible range: %@", NSStringFromRange(range));
var cells = [UICollectionViewLayoutAttributes]() //NSMutableArray(capacity: range.length)
for (var index=0,item=range.location; item < (range.location + range.length); item++, index++)
{
cells.append(self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: item, inSection: 0))!)
}
return cells as [UICollectionViewLayoutAttributes];
}
override func collectionViewContentSize() -> CGSize {
return collectionViewSize((collectionView?.bounds)!, count: (collectionView?.numberOfItemsInSection(0))!, m: metrics)
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true;
}
//MARK: - Postioning
//MARK: Cell visibility
func rangeForVisibleCells(rect:CGRect,count:NSInteger,m:PassbookLayoutMetrics)->NSRange{
var min:NSInteger = NSInteger(floor(rect.origin.y/(m.collapsed.size.height - m.collapsed.overlap)))
var max:NSInteger = NSInteger(ceil((rect.origin.y + rect.size.height)/(m.collapsed.size.height - m.collapsed.overlap)))
max = (max > count) ? count : max;
min = (min < 0) ? 0 : min;
min = (min < max) ? min : max;
let r = NSMakeRange(min, max-min);
return r;
}
func collectionViewSize(bounds:CGRect,count:NSInteger,m:PassbookLayoutMetrics )->CGSize{
return CGSizeMake(bounds.size.width, CGFloat(count)*(m.collapsed.size.height-m.collapsed.overlap));
}
//MARK: Cell positioning
/// Normal collapsed cell, with bouncy animations on top
func frameForPassAtIndex(indexPath:NSIndexPath,isLastCell:Bool,b:CGRect,m:PassbookLayoutMetrics,e:PassbookLayoutEffects)->CGRect{
var f:CGRect = CGRectZero
f.origin.x = (b.size.width - m.normal.size.width) / 2.0
f.origin.y = CGFloat(indexPath.item)*(m.collapsed.size.height - m.collapsed.overlap)
// The default size is the normal size
f.size = m.collapsed.size
if (b.origin.y < 0 && e.inheritance > 0.0 && e.bouncesTop)
{
// Bouncy effect on top (works only on constant invalidation)
if (indexPath.section == 0 && indexPath.item == 0)
{
// Keep stuck at top
f.origin.y = b.origin.y * e.inheritance/2.0
f.size.height = m.collapsed.size.height - b.origin.y * (1 + e.inheritance)
}
else
{
// Displace in stepping amounts factored by resitatnce
f.origin.y -= b.origin.y * CGFloat(indexPath.item) * e.inheritance
f.size.height -= b.origin.y * e.inheritance
}
}
else if (b.origin.y > 0)
{
// Stick to top
if (f.origin.y < b.origin.y && e.sticksTop)
{
f.origin.y = b.origin.y
}
}
// Edge case, if it's the last cell, display in full height, to avoid any issues.
if (isLastCell)
{
f.size = m.normal.size
}
return f
}
/// Centered cell
func frameForSelectedPass(b:CGRect,m:PassbookLayoutMetrics)->CGRect{
var f:CGRect = CGRectZero
f.size = m.normal.size
f.origin.x = (b.size.width - f.size.width ) / 2.0
f.origin.y = b.origin.y + (b.size.height - f.size.height) / 2.0
return f
}
/// Bottom-stack cell
func frameForUnselectedPass(indexPath:NSIndexPath,indexPathSelected:NSIndexPath,b:CGRect,m:PassbookLayoutMetrics)->CGRect{
var f:CGRect = CGRectZero
f.size = m.collapsed.size
f.origin.x = (b.size.width - m.normal.size.width) / 2.0
f.origin.y = b.origin.y + b.size.height - m.bottomStackedTotalHeight + m.bottomStackedHeight*CGFloat(indexPath.item - indexPathSelected.item)
return f
}
//MARK: z-index
func zIndexForPassAtIndex(indexPath:NSIndexPath)->NSInteger
{
return indexPath.item
}
//MARK: - Accessors
func setMetrics(metrics:PassbookLayoutMetrics){
self.metrics = metrics
self.invalidateLayout()
}
func setEffects(effects:PassbookLayoutEffects){
self.effects = effects
self.invalidateLayout()
}
}