forked from indragiek/BCCollectionView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCCollectionViewLayoutManager.m
122 lines (103 loc) · 3.41 KB
/
BCCollectionViewLayoutManager.m
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
// Created by Pieter Omvlee on 15/02/2011.
// Copyright 2011 Bohemian Coding. All rights reserved.
#import "BCCollectionViewLayoutManager.h"
#import "BCCollectionView.h"
@implementation BCCollectionViewLayoutManager
- (id)initWithCollectionView:(BCCollectionView *)aCollectionView
{
self = [super init];
if (self) {
collectionView = aCollectionView;
numberOfRows = -1;
}
return self;
}
- (void)willReload
{
numberOfRows = -1;
}
#pragma mark -
#pragma mark Primitives
- (NSUInteger)numberOfRows
{
if (numberOfRows == -1) {
NSInteger leftOver = [[collectionView contentArray] count];
numberOfRows = 0;
while (leftOver > 0) {
leftOver -= [self numberOfItemsAtRow:numberOfRows];
numberOfRows++;
}
}
return numberOfRows;
}
- (NSUInteger)numberOfItemsAtRow:(NSInteger)rowIndex
{
return MAX(1, [collectionView frame].size.width/[self cellSize].width);
}
- (NSSize)cellSize
{
return [collectionView cellSize];
}
- (NSUInteger)rowOfItemAtIndex:(NSInteger)anIndex
{
NSInteger rowIndex = 0;
while (anIndex >= [self numberOfItemsAtRow:rowIndex]) {
anIndex -= [self numberOfItemsAtRow:rowIndex];
rowIndex++;
}
return rowIndex;
}
- (NSUInteger)indexOfItemAtPointOrClosestGuess:(NSPoint)p
{
NSUInteger rowIndex = (int)p.y / [self cellSize].height;
NSInteger rowFirstIndex = 0;
for (NSInteger i=0; i<rowIndex; i++)
rowFirstIndex += [self numberOfItemsAtRow:i];
NSInteger gap = (NSWidth([collectionView visibleRect])-[self numberOfItemsAtRow:rowIndex]*[self cellSize].width)/([self numberOfItemsAtRow:rowIndex]-1);
NSUInteger index = rowFirstIndex + MIN([self numberOfItemsAtRow:rowIndex]-1, p.x / ([self cellSize].width+gap));
if (index >= [[collectionView contentArray] count])
return NSNotFound;
else
return index;
}
- (NSUInteger)indexOfItemAtPoint:(NSPoint)p
{
NSUInteger rowIndex = (int)p.y / [self cellSize].height;
NSInteger gap = (NSWidth([collectionView visibleRect])-[self numberOfItemsAtRow:rowIndex]*[self cellSize].width)/([self numberOfItemsAtRow:rowIndex]-1);
if (p.x > ([self cellSize].width+gap) * [self numberOfItemsAtRow:rowIndex])
return NSNotFound;
return [self indexOfItemAtPointOrClosestGuess:p];
}
- (NSUInteger)indexOfItemContentRectAtPoint:(NSPoint)p
{
NSUInteger index = [self indexOfItemAtPoint:p];
if (index != NSNotFound) {
if (NSPointInRect(p, [self contentRectOfItemAtIndex:index]))
return index;
else
return NSNotFound;
}
return index;
}
- (NSRect)rectOfItemAtIndex:(NSUInteger)anIndex
{
NSSize cellSize = [self cellSize];
NSInteger rowIndex = 0;
NSInteger columnIndex = anIndex;
while (columnIndex >= [self numberOfItemsAtRow:rowIndex]) {
columnIndex -= [self numberOfItemsAtRow:rowIndex];
rowIndex++;
}
NSInteger gap = (NSWidth([collectionView visibleRect])-[self numberOfItemsAtRow:rowIndex]*cellSize.width)/([self numberOfItemsAtRow:rowIndex]-1);
return NSMakeRect(columnIndex*(cellSize.width+gap), rowIndex*cellSize.height, cellSize.width, cellSize.height);
}
- (NSRect)contentRectOfItemAtIndex:(NSUInteger)anIndex
{
NSRect rect = [self rectOfItemAtIndex:anIndex];
if ([[collectionView delegate] respondsToSelector:@selector(insetMarginForSelectingItemsInCollectionView:)]) {
NSSize inset = [[collectionView delegate] insetMarginForSelectingItemsInCollectionView:collectionView];
return NSInsetRect(rect, inset.width, inset.height);
} else
return rect;
}
@end