forked from chiahsien/CHTCollectionViewWaterfallLayout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHTCollectionViewWaterfallLayout.swift
executable file
·387 lines (328 loc) · 15 KB
/
CHTCollectionViewWaterfallLayout.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// CHTCollectionViewWaterfallLayout.swift
// PinterestSwift
//
// Created by Nicholas Tau on 6/30/14.
// Copyright (c) 2014 Nicholas Tau. All rights reserved.
//
import Foundation
import UIKit
@objc protocol CHTCollectionViewDelegateWaterfallLayout: UICollectionViewDelegate{
func collectionView (collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
optional func colletionView (collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
heightForHeaderInSection section: NSInteger) -> CGFloat
optional func colletionView (collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
heightForFooterInSection section: NSInteger) -> CGFloat
optional func colletionView (collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAtIndex section: NSInteger) -> UIEdgeInsets
optional func colletionView (collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAtIndex section: NSInteger) -> CGFloat
}
enum CHTCollectionViewWaterfallLayoutItemRenderDirection : NSInteger{
case CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst
case CHTCollectionViewWaterfallLayoutItemRenderDirectionLeftToRight
case CHTCollectionViewWaterfallLayoutItemRenderDirectionRightToLeft
}
class CHTCollectionViewWaterfallLayout : UICollectionViewLayout{
let CHTCollectionElementKindSectionHeader = "CHTCollectionElementKindSectionHeader"
let CHTCollectionElementKindSectionFooter = "CHTCollectionElementKindSectionFooter"
var columnCount : NSInteger{
didSet{
invalidateLayout()
}}
var minimumColumnSpacing : CGFloat{
didSet{
invalidateLayout()
}}
var minimumInteritemSpacing : CGFloat{
didSet{
invalidateLayout()
}}
var headerHeight : CGFloat{
didSet{
invalidateLayout()
}}
var footerHeight : CGFloat{
didSet{
invalidateLayout()
}}
var sectionInset : UIEdgeInsets{
didSet{
invalidateLayout()
}}
var itemRenderDirection : CHTCollectionViewWaterfallLayoutItemRenderDirection{
didSet{
invalidateLayout()
}}
// private property and method above.
weak var delegate : CHTCollectionViewDelegateWaterfallLayout?{
get{
return self.collectionView!.delegate as? CHTCollectionViewDelegateWaterfallLayout
}
}
var columnHeights : NSMutableArray
var sectionItemAttributes : NSMutableArray
var allItemAttributes : NSMutableArray
var headersAttributes : NSMutableDictionary
var footersAttributes : NSMutableDictionary
var unionRects : NSMutableArray
let unionSize = 20
override init(){
self.headerHeight = 0.0
self.footerHeight = 0.0
self.columnCount = 2
self.minimumInteritemSpacing = 10
self.minimumColumnSpacing = 10
self.sectionInset = UIEdgeInsetsZero
self.itemRenderDirection =
CHTCollectionViewWaterfallLayoutItemRenderDirection.CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst
headersAttributes = NSMutableDictionary()
footersAttributes = NSMutableDictionary()
unionRects = NSMutableArray()
columnHeights = NSMutableArray()
allItemAttributes = NSMutableArray()
sectionItemAttributes = NSMutableArray()
super.init()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func itemWidthInSectionAtIndex (section : NSInteger) -> CGFloat {
var insets : UIEdgeInsets
if let sectionInsets = self.delegate?.colletionView?(self.collectionView!, layout: self, insetForSectionAtIndex: section){
insets = sectionInsets
}else{
insets = self.sectionInset
}
let width:CGFloat = self.collectionView!.frame.size.width - sectionInset.left-sectionInset.right
let spaceColumCount:CGFloat = CGFloat(self.columnCount-1)
return floor((width - (spaceColumCount*self.minimumColumnSpacing)) / CGFloat(self.columnCount))
}
override func prepareLayout(){
super.prepareLayout()
let numberOfSections = self.collectionView!.numberOfSections()
if numberOfSections == 0 {
return
}
self.headersAttributes.removeAllObjects()
self.footersAttributes.removeAllObjects()
self.unionRects.removeAllObjects()
self.columnHeights.removeAllObjects()
self.allItemAttributes.removeAllObjects()
self.sectionItemAttributes.removeAllObjects()
var idx = 0
while idx<self.columnCount{
self.columnHeights.addObject(0)
idx++
}
var top : CGFloat = 0.0
var attributes = UICollectionViewLayoutAttributes()
for var section = 0; section < numberOfSections; ++section{
/*
* 1. Get section-specific metrics (minimumInteritemSpacing, sectionInset)
*/
var minimumInteritemSpacing : CGFloat
if let miniumSpaceing = self.delegate?.colletionView?(self.collectionView!, layout: self, minimumInteritemSpacingForSectionAtIndex: section){
minimumInteritemSpacing = miniumSpaceing
}else{
minimumInteritemSpacing = self.minimumColumnSpacing
}
var sectionInsets : UIEdgeInsets
if let insets = self.delegate?.colletionView?(self.collectionView!, layout: self, insetForSectionAtIndex: section){
sectionInsets = insets
}else{
sectionInsets = self.sectionInset
}
let width = self.collectionView!.frame.size.width - sectionInset.left - sectionInset.right
let spaceColumCount = CGFloat(self.columnCount-1)
let itemWidth = floor((width - (spaceColumCount*self.minimumColumnSpacing)) / CGFloat(self.columnCount))
/*
* 2. Section header
*/
var heightHeader : CGFloat
if let height = self.delegate?.colletionView?(self.collectionView!, layout: self, heightForHeaderInSection: section){
heightHeader = height
}else{
heightHeader = self.headerHeight
}
if heightHeader > 0 {
attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: CHTCollectionElementKindSectionHeader, withIndexPath: NSIndexPath(forRow: 0, inSection: section))
attributes.frame = CGRectMake(0, top, self.collectionView!.frame.size.width, heightHeader)
self.headersAttributes.setObject(attributes, forKey: (section))
self.allItemAttributes.addObject(attributes)
top = CGRectGetMaxY(attributes.frame)
}
top += sectionInset.top
for var idx = 0; idx < self.columnCount; idx++ {
self.columnHeights[idx]=top;
}
/*
* 3. Section items
*/
let itemCount = self.collectionView!.numberOfItemsInSection(section)
let itemAttributes = NSMutableArray(capacity: itemCount)
// Item will be put into shortest column.
for var idx = 0; idx < itemCount; idx++ {
let indexPath = NSIndexPath(forItem: idx, inSection: section)
let columnIndex = self.nextColumnIndexForItem(idx)
let xOffset = sectionInset.left + (itemWidth + self.minimumColumnSpacing) * CGFloat(columnIndex)
let yOffset = self.columnHeights.objectAtIndex(columnIndex).doubleValue
let itemSize = self.delegate?.collectionView(self.collectionView!, layout: self, sizeForItemAtIndexPath: indexPath)
var itemHeight : CGFloat = 0.0
if itemSize?.height > 0 && itemSize?.width > 0 {
itemHeight = floor(itemSize!.height*itemWidth/itemSize!.width)
}
attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = CGRectMake(xOffset, CGFloat(yOffset), itemWidth, itemHeight)
itemAttributes.addObject(attributes)
self.allItemAttributes.addObject(attributes)
self.columnHeights[columnIndex]=CGRectGetMaxY(attributes.frame) + minimumInteritemSpacing;
}
self.sectionItemAttributes.addObject(itemAttributes)
/*
* 4. Section footer
*/
var footerHeight : CGFloat = 0.0
let columnIndex = self.longestColumnIndex()
top = CGFloat(self.columnHeights.objectAtIndex(columnIndex).floatValue) - minimumInteritemSpacing + sectionInset.bottom
if let height = self.delegate?.colletionView?(self.collectionView!, layout: self, heightForFooterInSection: section){
footerHeight = height
}else{
footerHeight = self.footerHeight
}
if footerHeight > 0 {
attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: CHTCollectionElementKindSectionFooter, withIndexPath: NSIndexPath(forItem: 0, inSection: section))
attributes.frame = CGRectMake(0, top, self.collectionView!.frame.size.width, footerHeight)
self.footersAttributes.setObject(attributes, forKey: section)
self.allItemAttributes.addObject(attributes)
top = CGRectGetMaxY(attributes.frame)
}
for var idx = 0; idx < self.columnCount; idx++ {
self.columnHeights[idx] = top
}
}
idx = 0;
let itemCounts = self.allItemAttributes.count
while(idx < itemCounts){
var rect1 = self.allItemAttributes.objectAtIndex(idx).frame as CGRect
idx = min(idx + unionSize, itemCounts) - 1
var rect2 = self.allItemAttributes.objectAtIndex(idx).frame as CGRect
self.unionRects.addObject(NSValue(CGRect:CGRectUnion(rect1,rect2)))
idx++
}
}
override func collectionViewContentSize() -> CGSize{
var numberOfSections = self.collectionView!.numberOfSections()
if numberOfSections == 0{
return CGSizeZero
}
var contentSize = self.collectionView!.bounds.size as CGSize
let height = self.columnHeights.objectAtIndex(0) as NSNumber
contentSize.height = CGFloat(height.doubleValue)
return contentSize
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes!{
if indexPath.section >= self.sectionItemAttributes.count{
return nil
}
if indexPath.item >= self.sectionItemAttributes.objectAtIndex(indexPath.section).count{
return nil;
}
var list = self.sectionItemAttributes.objectAtIndex(indexPath.section) as NSArray
return list.objectAtIndex(indexPath.item) as UICollectionViewLayoutAttributes
}
override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes{
var attribute = UICollectionViewLayoutAttributes()
if elementKind == CHTCollectionElementKindSectionHeader{
attribute = self.headersAttributes.objectForKey(indexPath.section) as UICollectionViewLayoutAttributes
}else if elementKind == CHTCollectionElementKindSectionFooter{
attribute = self.footersAttributes.objectForKey(indexPath.section) as UICollectionViewLayoutAttributes
}
return attribute
}
override func layoutAttributesForElementsInRect (rect : CGRect) -> [AnyObject] {
var i = 0
var begin = 0, end = self.unionRects.count
var attrs = NSMutableArray()
for var i = 0; i < end; i++ {
if CGRectIntersectsRect(rect, self.unionRects.objectAtIndex(i).CGRectValue()){
begin = i * unionSize;
break
}
}
for var i = self.unionRects.count - 1; i>=0; i-- {
if CGRectIntersectsRect(rect, self.unionRects.objectAtIndex(i).CGRectValue()){
end = min((i+1)*unionSize,self.allItemAttributes.count)
break
}
}
for var i = begin; i < end; i++ {
var attr = self.allItemAttributes.objectAtIndex(i) as UICollectionViewLayoutAttributes
if CGRectIntersectsRect(rect, attr.frame) {
attrs.addObject(attr)
}
}
return NSArray(array: attrs)
}
override func shouldInvalidateLayoutForBoundsChange (newBounds : CGRect) -> Bool {
var oldBounds = self.collectionView!.bounds
if CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds){
return true
}
return false
}
/**
* Find the shortest column.
*
* @return index for the shortest column
*/
func shortestColumnIndex () -> NSInteger {
var index = 0
var shorestHeight = MAXFLOAT
self.columnHeights.enumerateObjectsUsingBlock({(object : AnyObject!, idx : NSInteger,pointer :UnsafeMutablePointer<ObjCBool>) in
let height = object.floatValue
if (height<shorestHeight){
shorestHeight = height
index = idx
}
})
return index
}
/**
* Find the longest column.
*
* @return index for the longest column
*/
func longestColumnIndex () -> NSInteger {
var index = 0
var longestHeight:CGFloat = 0.0
self.columnHeights.enumerateObjectsUsingBlock({(object : AnyObject!, idx : NSInteger,pointer :UnsafeMutablePointer<ObjCBool>) in
let height = CGFloat(object.floatValue)
if (height > longestHeight){
longestHeight = height
index = idx
}
})
return index
}
/**
* Find the index for the next column.
*
* @return index for the next column
*/
func nextColumnIndexForItem (item : NSInteger) -> Int {
var index = 0
switch (self.itemRenderDirection){
case .CHTCollectionViewWaterfallLayoutItemRenderDirectionShortestFirst :
index = self.shortestColumnIndex()
case .CHTCollectionViewWaterfallLayoutItemRenderDirectionLeftToRight :
index = (item%self.columnCount)
case .CHTCollectionViewWaterfallLayoutItemRenderDirectionRightToLeft:
index = (self.columnCount - 1) - (item % self.columnCount);
default:
index = self.shortestColumnIndex()
}
return index
}
}