Permalink
Browse files

Performance enhancements.

  • Loading branch information...
1 parent ab00f84 commit db6118ed2a0564cc500033b8a924d139efce58da @camh committed Feb 25, 2010
Showing with 209 additions and 200 deletions.
  1. +14 −10 CHGridLayout/CHGridLayout.h
  2. +66 −48 CHGridLayout/CHGridLayout.m
  3. +2 −0 CHGridView.h
  4. +71 −74 CHGridView.m
  5. +27 −36 CHImageTileView.m
  6. +15 −15 CHSectionTitleView.m
  7. +14 −17 CHTileView.m
@@ -30,8 +30,9 @@ static CHSectionRange CHSectionRangeMake(int start, int end){
//-----------
@interface CHGridLayout : NSObject {
- NSMutableArray *_index;
- NSMutableArray *_sectionTitles;
+ NSMutableArray *index;
+ NSMutableArray *sectionTitles;
+ NSMutableArray *justTiles;
float gridWidth;
float contentHeight;
@@ -44,14 +45,17 @@ static CHSectionRange CHSectionRangeMake(int start, int end){
BOOL dynamicallyResizeTilesToFillSpace;
}
-@property (nonatomic) float gridWidth;
-@property (nonatomic, readonly) CGFloat contentHeight;
-@property (nonatomic) CGSize padding;
-@property (nonatomic) int perLine;
-@property (nonatomic) float preLoadMultiplier;
-@property (nonatomic) float rowHeight;
-@property (nonatomic) float sectionTitleHeight;
-@property (nonatomic) BOOL dynamicallyResizeTilesToFillSpace;
+@property (nonatomic, readonly) NSMutableArray *index;
+@property (nonatomic, readonly) NSMutableArray *justTiles;
+
+@property (nonatomic) float gridWidth;
+@property (nonatomic, readonly) CGFloat contentHeight;
+@property (nonatomic) CGSize padding;
+@property (nonatomic) int perLine;
+@property (nonatomic) float preLoadMultiplier;
+@property (nonatomic) float rowHeight;
+@property (nonatomic) float sectionTitleHeight;
+@property (nonatomic) BOOL dynamicallyResizeTilesToFillSpace;
- (void)setSections:(int)sections;
- (void)setNumberOfTiles:(int)tiles ForSectionIndex:(int)section;
View
@@ -12,39 +12,48 @@
#import "CHGridLayoutTile.h"
#import "CHGridLayoutSection.h"
#include <sys/types.h>
-#include <sys/sysctl.h>
+#include <sys/sysctl.h>
+
+#define SLOW_DEVICE_PRELOAD 2.0f
@implementation CHGridLayout
-@synthesize gridWidth, contentHeight, padding, perLine, preLoadMultiplier, rowHeight, sectionTitleHeight, dynamicallyResizeTilesToFillSpace;
+@synthesize index, justTiles, gridWidth, contentHeight, padding, perLine, preLoadMultiplier, rowHeight, sectionTitleHeight, dynamicallyResizeTilesToFillSpace;
- (id)init{
if(self = [super init]){
- if(_index == nil)
- _index = [[NSMutableArray alloc] init];
+ if(index == nil)
+ index = [[NSMutableArray alloc] init];
+
+ if(sectionTitles == nil)
+ sectionTitles = [[NSMutableArray alloc] init];
- if(_sectionTitles == nil)
- _sectionTitles = [[NSMutableArray alloc] init];
+ if(justTiles == nil)
+ justTiles = [[NSMutableArray alloc] init];
- preLoadMultiplier = 2.0;
+ preLoadMultiplier = 5.0f;
- contentHeight = 0.0;
- rowHeight = 0.0;
+ contentHeight = 0.0f;
+ rowHeight = 0.0f;
}
return self;
}
- (void)dealloc{
- [_sectionTitles release];
- [_index release];
+ [justTiles release];
+ [sectionTitles release];
+ [index release];
[super dealloc];
}
#pragma mark setters
- (void)setRowHeight:(CGFloat)f{
rowHeight = f;
-
pixelMargin = f * preLoadMultiplier;
+}
+
+- (void)setPreLoadMultiplier:(float)f{
+ preLoadMultiplier = f;
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
@@ -53,9 +62,9 @@ - (void)setRowHeight:(CGFloat)f{
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding];
free(machine);
- if([platform isEqualToString:@"iPhone1,1"]) pixelMargin = f * 0.5;
- if([platform isEqualToString:@"iPhone1,2"]) pixelMargin = f * 0.5;
- if([platform isEqualToString:@"iPod1,1"]) pixelMargin = f * 0.5;
+ if([platform isEqualToString:@"iPhone1,1"]) preLoadMultiplier = SLOW_DEVICE_PRELOAD;
+ if([platform isEqualToString:@"iPhone1,2"]) preLoadMultiplier = SLOW_DEVICE_PRELOAD;
+ if([platform isEqualToString:@"iPod1,1"]) preLoadMultiplier = SLOW_DEVICE_PRELOAD;
}
- (void)setSections:(int)sections{
@@ -65,60 +74,61 @@ - (void)setSections:(int)sections{
for(i = 0; i < sections; i++){
NSMutableArray *section = [NSMutableArray array];
CHGridLayoutSection *section2 = [[CHGridLayoutSection alloc] initWithSection:i];
- [_index addObject:section];
- [_sectionTitles addObject:section2];
+ [index addObject:section];
+ [sectionTitles addObject:section2];
[section2 release];
}
}
- (void)setNumberOfTiles:(int)tiles ForSectionIndex:(int)section{
- if(section < 0 || section >= _index.count) return;
+ if(section < 0 || section >= index.count) return;
int i;
for(i = 0; i < tiles; i++){
CHGridLayoutTile *tile = [[CHGridLayoutTile alloc] initWithIndexPath:CHGridIndexPathMake(section, i)];
- [[_index objectAtIndex:section] addObject:tile];
+ [[index objectAtIndex:section] addObject:tile];
+ [justTiles addObject:tile];
[tile release];
}
}
#pragma mark data & layout
- (void)clearData{
- [_index removeAllObjects];
- [_sectionTitles removeAllObjects];
- contentHeight = 0.0;
+ [index removeAllObjects];
+ [sectionTitles removeAllObjects];
+ contentHeight = 0.0f;
}
- (void)updateLayout{
- int sections = _index.count;
+ int sections = index.count;
float perLineFloat = perLine;
- for(NSMutableArray *array in _index){
+ for(NSMutableArray *array in index){
int numberOfTilesInSection = [array count];
- contentHeight += ceil(numberOfTilesInSection / perLineFloat) * rowHeight;
+ contentHeight += ceilf(numberOfTilesInSection / perLineFloat) * rowHeight;
}
if(sections > 1) contentHeight += (sectionTitleHeight * sections) + ((sections - 1) * padding.height);
contentHeight += padding.height;
int i;
- for(i = 0; i < _index.count; i++){
- CHGridLayoutSection *section = [_sectionTitles objectAtIndex:i];
+ for(i = 0; i < index.count; i++){
+ CHGridLayoutSection *section = [sectionTitles objectAtIndex:i];
CHGridLayoutSection *previousSection = nil;
- if(i > 0) previousSection = [_sectionTitles objectAtIndex:(i - 1)];
- NSMutableArray *tilesForSection = [_index objectAtIndex:i];
+ if(i > 0) previousSection = [sectionTitles objectAtIndex:(i - 1)];
+ NSMutableArray *tilesForSection = [index objectAtIndex:i];
if(sections > 1){
float previousY = 0.0;
if(previousSection != nil) previousY = previousSection.yCoordinate;
int numberOfTilesInPreviousSection = 0;
- if(i > 0) numberOfTilesInPreviousSection = [[_index objectAtIndex:(i - 1)] count];
+ if(i > 0) numberOfTilesInPreviousSection = [[index objectAtIndex:(i - 1)] count];
float sectionYPadding = 0.0f;
if(i > 0) sectionYPadding = padding.height + sectionTitleHeight;
- [section setYCoordinate:ceil(numberOfTilesInPreviousSection / perLineFloat) * rowHeight + sectionYPadding + previousY];
+ [section setYCoordinate:ceilf(numberOfTilesInPreviousSection / perLineFloat) * rowHeight + sectionYPadding + previousY];
}else{
section = nil;
}
@@ -128,10 +138,10 @@ - (void)updateLayout{
if(section != nil) y = section.yCoordinate + sectionTitleHeight;
float rowXPadding = (padding.width * perLineFloat) + padding.width;
- float row = floor(tile.indexPath.tileIndex / perLine);
+ float row = floorf(tile.indexPath.tileIndex / perLine);
int rowIndex = tile.indexPath.tileIndex - (row * perLine);
- float width = ceil((gridWidth - rowXPadding) / perLine);
+ float width = ceilf((gridWidth - rowXPadding) / perLine);
float height = rowHeight - padding.height;
[tile setRect:CGRectMake(padding.width + (rowIndex * width) + (rowIndex * padding.width), row * rowHeight + y + padding.height, width, height)];
@@ -144,7 +154,7 @@ - (void)updateLayout{
- (int)sectionIndexForContentOffset:(CGFloat)offset{
int sectionIndex = 0;
- for(CHGridLayoutSection *section in _sectionTitles){
+ for(CHGridLayoutSection *section in sectionTitles){
if(section.yCoordinate <= offset && offset > 0){
sectionIndex = section.section;
}
@@ -154,7 +164,7 @@ - (int)sectionIndexForContentOffset:(CGFloat)offset{
}
- (CGFloat)yCoordinateForTitleOfSection:(int)section{
- return [[_sectionTitles objectAtIndex:section] yCoordinate];
+ return [[sectionTitles objectAtIndex:section] yCoordinate];
}
- (CHSectionRange)sectionRangeForContentOffset:(CGFloat)offset andHeight:(CGFloat)height{
@@ -164,14 +174,20 @@ - (CHSectionRange)sectionRangeForContentOffset:(CGFloat)offset andHeight:(CGFloa
BOOL firstRun = YES;
int currentSection = [self sectionIndexForContentOffset:offset];
- for(CHGridLayoutSection *section in _sectionTitles){
- if(firstRun && section.section >= currentSection){
- start = section.section;
+ float firstY = (offset - pixelMargin);
+ float secondY = (offset + height + pixelMargin);
+
+ for(CHGridLayoutSection *section in sectionTitles){
+ int s = section.section;
+ float sy = section.yCoordinate;
+
+ if(firstRun && s >= currentSection){
+ start = s;
firstRun = NO;
}
- if(section.yCoordinate > (offset - pixelMargin) && section.yCoordinate < (offset + height + pixelMargin)){
- end = section.section;
+ if(sy > firstY && sy < secondY){
+ end = s;
}
if(start > end) end = start;
@@ -183,7 +199,7 @@ - (CHSectionRange)sectionRangeForContentOffset:(CGFloat)offset andHeight:(CGFloa
- (CHGridIndexPath)closestIndexPathToContentOffsetY:(CGFloat)offset{
CHGridLayoutTile *closestTile = nil;
- for(NSMutableArray *section in _index){
+ for(NSMutableArray *section in index){
for(CHGridLayoutTile *tile in section){
if(tile.rect.origin.y > offset){
if(closestTile == nil){
@@ -196,25 +212,27 @@ - (CHGridIndexPath)closestIndexPathToContentOffsetY:(CGFloat)offset{
}
if(closestTile != nil){
- NSLog(@"closest indexPath.section = %i tileIndex = %i", closestTile.indexPath.section, closestTile.indexPath.tileIndex);
return [closestTile indexPath];
}
return CHGridIndexPathMake(0, 0);
}
- (CGRect)tileFrameForIndexPath:(CHGridIndexPath)indexPath{
- NSMutableArray *sectionTiles = [_index objectAtIndex:indexPath.section];
+ NSMutableArray *sectionTiles = [index objectAtIndex:indexPath.section];
return [[sectionTiles objectAtIndex:indexPath.tileIndex] rect];
}
- (CHGridIndexRange)rangeOfVisibleIndexesForContentOffset:(CGFloat)offset andHeight:(CGFloat)height{
BOOL first = NO;
CHGridIndexRange indexRange = {CHGridIndexPathMake(0, 0),CHGridIndexPathMake(0, 0)};
- for(NSMutableArray *sectionArray in _index){
+ float firstY = (height + offset + pixelMargin);
+ float secondY = offset - pixelMargin;
+
+ for(NSMutableArray *sectionArray in index){
for(CHGridLayoutTile *tile in sectionArray){
- if(tile.rect.origin.y < (height + offset + pixelMargin) && tile.rect.origin.y + tile.rect.size.height >= offset - pixelMargin){
- if(first == NO){
+ if(tile.rect.origin.y < firstY && tile.rect.origin.y + tile.rect.size.height >= secondY){
+ if(!first){
indexRange.start = [tile indexPath];
first = YES;
}
@@ -230,9 +248,9 @@ - (CHGridIndexRange)rangeOfVisibleIndexesForContentOffset:(CGFloat)offset andHei
- (CGRect)centerRect:(CGRect)smallerRect inLargerRect:(CGRect)largerRect roundUp:(BOOL)roundUp{
if (roundUp)
- return CGRectMake(ceil((largerRect.size.width - smallerRect.size.width) / 2), ceil((largerRect.size.height - smallerRect.size.height) / 2), smallerRect.size.width, smallerRect.size.height);
+ return CGRectMake(ceilf((largerRect.size.width - smallerRect.size.width) / 2), ceilf((largerRect.size.height - smallerRect.size.height) / 2), smallerRect.size.width, smallerRect.size.height);
- return CGRectMake(floor((largerRect.size.width - smallerRect.size.width) / 2), floor((largerRect.size.height - smallerRect.size.height) / 2), smallerRect.size.width, smallerRect.size.height);
+ return CGRectMake(floorf((largerRect.size.width - smallerRect.size.width) / 2), floorf((largerRect.size.height - smallerRect.size.height) / 2), smallerRect.size.width, smallerRect.size.height);
}
@end
View
@@ -47,8 +47,10 @@
int sections;
NSMutableArray *sectionCounts;
+ int maxReusable;
CHTileView *selectedTile;
+ BOOL isSlowDevice;
//settable properties
BOOL dynamicallyResizeTilesToFillSpace;
Oops, something went wrong.

0 comments on commit db6118e

Please sign in to comment.