-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCCollectionViewLayoutManager.m
126 lines (107 loc) · 3.11 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
123
124
125
126
// Created by Pieter Omvlee on 15/02/2011.
// Copyright 2011 Bohemian Coding. All rights reserved.
#import "BCCollectionViewLayoutManager.h"
#import "BCCollectionView.h"
#import "BCCollectionViewGroup.h"
#import "BCCollectionViewLayoutItem.h"
@implementation BCCollectionViewLayoutManager
@synthesize itemLayouts;
- (id)initWithCollectionView:(BCCollectionView *)aCollectionView
{
self = [super init];
if (self) {
collectionView = aCollectionView;
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
}
return self;
}
- (void)cancelItemEnumerator
{
[queue cancelAllOperations];
}
- (void)enumerateItems:(BCCollectionViewLayoutOperationIterator)itemIterator completionBlock:(dispatch_block_t)completionBlock
{
BCCollectionViewLayoutOperation *operation = [[BCCollectionViewLayoutOperation alloc] init];
[operation setCollectionView:collectionView];
[operation setLayoutCallBack:itemIterator];
[operation setLayoutCompletionBlock:completionBlock];
// if ([queue operationCount] > 10)
[queue cancelAllOperations];
[queue addOperation:[operation autorelease]];
}
- (void)dealloc
{
[itemLayouts release];
[queue release];
[super dealloc];
}
#pragma mark -
#pragma mark Primitives
- (NSUInteger)maximumNumberOfItemsPerRow
{
return MAX(1, [collectionView frame].size.width/[self cellSize].width);
}
- (NSSize)cellSize
{
return [collectionView cellSize];
}
#pragma mark -
#pragma mark Rows and Columns
- (NSPoint)rowAndColumnPositionOfItemAtIndex:(NSUInteger)anIndex
{
if ([itemLayouts count] > anIndex) {
BCCollectionViewLayoutItem *itemLayout = [itemLayouts objectAtIndex:anIndex];
return NSMakePoint(itemLayout.columnIndex, itemLayout.rowIndex);
} else
return NSZeroPoint;
}
- (NSUInteger)indexOfItemAtRow:(NSUInteger)rowIndex column:(NSUInteger)colIndex
{
__block NSUInteger index = NSNotFound;
[itemLayouts enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(BCCollectionViewLayoutItem *item, NSUInteger idx, BOOL *stop) {
if ([item rowIndex] == rowIndex && [item columnIndex] == colIndex) {
index = [item itemIndex];
*stop = YES;
}
}];
return index;
}
#pragma mark -
#pragma mark From Point to Index
- (NSUInteger)indexOfItemAtPoint:(NSPoint)p
{
NSInteger count = [itemLayouts count];
for (NSInteger i=0; i<count; i++)
if (NSPointInRect(p, [[itemLayouts objectAtIndex:i] itemRect]))
return i;
return NSNotFound;
}
- (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;
}
#pragma mark -
#pragma mark From Index to Rect
- (NSRect)rectOfItemAtIndex:(NSUInteger)anIndex
{
if (anIndex < [itemLayouts count])
return [[itemLayouts objectAtIndex:anIndex] itemRect];
else
return NSZeroRect;
}
- (NSRect)contentRectOfItemAtIndex:(NSUInteger)anIndex
{
if (anIndex < [itemLayouts count])
return [[itemLayouts objectAtIndex:anIndex] itemContentRect];
else
return NSZeroRect;
}
@end