Skip to content
Browse files

Optimises drawing itemSelections which hugely speeds up scrolling whe…

…n items are selected
  • Loading branch information...
1 parent 0106cd3 commit bea03a521bff60afe5e8df7cc6a48cbb2f6a0849 @pieteromvlee pieteromvlee committed
Showing with 33 additions and 13 deletions.
  1. +5 −2 BCCollectionView.h
  2. +28 −11 BCCollectionView.m
View
7 BCCollectionView.h
@@ -18,8 +18,8 @@
NSMutableArray *reusableViewControllers;
NSMutableDictionary *visibleViewControllers;
- NSMutableIndexSet *selectionIndexes;
- NSIndexSet *originalSelectionIndexes;
+ NSMutableIndexSet *selectionIndexes;
+ NSIndexSet *originalSelectionIndexes;
NSPoint mouseDownLocation;
NSPoint mouseDraggedLocation;
@@ -42,6 +42,8 @@
//Managing Selections
- (void)selectItemAtIndex:(NSUInteger)index;
+- (void)selectItemAtIndex:(NSUInteger)index inBulk:(BOOL)bulk;
+
- (void)selectItemsAtIndexes:(NSIndexSet *)indexes;
- (void)deselectItemAtIndex:(NSUInteger)index;
- (void)deselectItemsAtIndexes:(NSIndexSet *)indexes;
@@ -55,6 +57,7 @@
- (NSIndexSet *)indexesOfItemsInRect:(NSRect)aRect;
- (NSUInteger)indexOfItemAtPoint:(NSPoint)p;
- (NSUInteger)indexOfItemAtPointOrClosestGuess:(NSPoint)p;
+- (NSViewController *)viewControllerForItemAtIndex:(NSUInteger)index;
//Querying ViewControllers
- (NSIndexSet *)indexesOfViewControllers;
View
39 BCCollectionView.m
@@ -79,12 +79,15 @@ - (BOOL)shoulDrawSelections
return YES;
}
-- (void)drawSelectionForItemAtIndex:(NSUInteger)index
+//- (void)drawSelectionForItemAtIndex:(NSUInteger)index
+
+- (void)drawItemSelectionInRect:(NSRect)aRect
{
- NSRect itemRect = NSInsetRect([self rectOfItemAtIndex:index], 10, 10);
-
- [[NSColor lightGrayColor] set];
- [[NSBezierPath bezierPathWithRoundedRect:itemRect xRadius:10 yRadius:10] fill];
+ NSRect insetRect = NSInsetRect(aRect, 10, 10);
+ if ([self needsToDrawRect:insetRect]) {
+ [[NSColor lightGrayColor] set];
+ [[NSBezierPath bezierPathWithRoundedRect:insetRect xRadius:10 yRadius:10] fill];
+ }
}
- (void)drawRect:(NSRect)dirtyRect
@@ -96,10 +99,10 @@ - (void)drawRect:(NSRect)dirtyRect
NSFrameRect(BCRectFromTwoPoints(mouseDownLocation, mouseDraggedLocation));
if ([selectionIndexes count] > 0 && [self shoulDrawSelections]) {
- [[self indexesOfItemsInRect:dirtyRect] enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
- if ([selectionIndexes containsIndex:idx])
- [self drawSelectionForItemAtIndex:idx];
- }];
+ for (NSNumber *number in visibleViewControllers) {
+ if ([selectionIndexes containsIndex:[number integerValue]])
+ [self drawItemSelectionInRect:[[[visibleViewControllers objectForKey:number] view] frame]];
+ }
}
}
@@ -190,6 +193,7 @@ - (NSRect)rectOfItemAtIndex:(NSUInteger)anIndex
NSSize cellSize = [self cellSize];
NSUInteger rowIndex = anIndex / [self numberOfItemsPerRow];
NSUInteger columnIndex = anIndex % [self numberOfItemsPerRow];
+
return NSMakeRect(columnIndex*cellSize.width, rowIndex*cellSize.height, cellSize.width, cellSize.height);
}
@@ -308,6 +312,11 @@ - (void)moveViewControllersToProperPosition
- (void)selectItemAtIndex:(NSUInteger)index
{
+ [self selectItemAtIndex:index inBulk:NO];
+}
+
+- (void)selectItemAtIndex:(NSUInteger)index inBulk:(BOOL)bulkSelecting
+{
if (index >= [contentArray count])
return;
@@ -325,14 +334,17 @@ - (void)selectItemAtIndex:(NSUInteger)index
if ([self shoulDrawSelections])
[self setNeedsDisplayInRect:[self rectOfItemAtIndex:index]];
}
- self.lastSelectionIndex = index;
+
+ if (!bulkSelecting)
+ self.lastSelectionIndex = index;
}
- (void)selectItemsAtIndexes:(NSIndexSet *)indexes
{
[indexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
- [self selectItemAtIndex:idx];
+ [self selectItemAtIndex:idx inBulk:YES];
}];
+ self.lastSelectionIndex = [indexes firstIndex];
}
- (void)deselectItemAtIndex:(NSUInteger)index
@@ -359,6 +371,11 @@ - (void)deselectAllItems
}];
}
+- (void)selectAll:(id)sender
+{
+ [self selectItemsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,[contentArray count])]];
+}
+
#pragma mark User-interaction
- (BOOL)acceptsFirstResponder

0 comments on commit bea03a5

Please sign in to comment.
Something went wrong with that request. Please try again.