Browse files

Added support to scroll to a tile at a specific index path.

  • Loading branch information...
1 parent 149ac66 commit e4596857f82ea2474d234e0775c6028d7e5f842c @camh committed Feb 23, 2010
Showing with 35 additions and 4 deletions.
  1. +2 −0 CHGridLayout/CHGridLayout.h
  2. +22 −0 CHGridLayout/CHGridLayout.m
  3. +2 −0 CHGridView.h
  4. +9 −2 CHGridView.m
  5. +0 −2 README.markdown
View
2 CHGridLayout/CHGridLayout.h
@@ -60,6 +60,8 @@ static CHSectionRange CHSectionRangeMake(int start, int end){
- (CGFloat)yCoordinateForTitleOfSection:(int)section;
- (CHSectionRange)sectionRangeForContentOffset:(CGFloat)offset andHeight:(CGFloat)height;
+
+- (CHGridIndexPath)closestIndexPathToContentOffsetY:(CGFloat)offset;
- (CGRect)tileFrameForIndexPath:(CHGridIndexPath)indexPath;
- (CHGridIndexRange)rangeOfVisibleIndexesForContentOffset:(CGFloat)offset andHeight:(CGFloat)height;
View
22 CHGridLayout/CHGridLayout.m
@@ -180,6 +180,28 @@ - (CHSectionRange)sectionRangeForContentOffset:(CGFloat)offset andHeight:(CGFloa
return CHSectionRangeMake(start, end);
}
+- (CHGridIndexPath)closestIndexPathToContentOffsetY:(CGFloat)offset{
+ CHGridLayoutTile *closestTile = nil;
+
+ for(NSMutableArray *section in _index){
+ for(CHGridLayoutTile *tile in section){
+ if(tile.rect.origin.y > offset){
+ if(closestTile == nil){
+ closestTile = tile;
+ }else if(tile.rect.origin.y < closestTile.rect.origin.y && closestTile.rect.origin.y < offset){
+ closestTile = tile;
+ }
+ }
+ }
+ }
+
+ 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];
return [[sectionTiles objectAtIndex:indexPath.tileIndex] rect];
View
2 CHGridView.h
@@ -86,6 +86,8 @@
- (CHTileView *)tileForIndexPath:(CHGridIndexPath)indexPath;
- (CHGridIndexPath)indexPathForPoint:(CGPoint)point;
+- (void)scrollToTileAtIndexPath:(CHGridIndexPath)indexPath animated:(BOOL)animated;
+
- (void)deselectTileAtIndexPath:(CHGridIndexPath)indexPath;
- (void)deselecSelectedTile;
View
11 CHGridView.m
@@ -25,7 +25,7 @@ - (void)calculateSectionTitleOffset;
@end
@implementation CHGridView
-@synthesize dynamicallyResizeTilesToFillSpace, allowsSelection, padding, rowHeight, perLine, sectionTitleHeight, shadowOffset, shadowColor, shadowBlur;
+@synthesize dynamicallyResizeTilesToFillSpace, allowsSelection, padding, preLoadMultiplier, rowHeight, perLine, sectionTitleHeight, shadowOffset, shadowColor, shadowBlur;
- (id)init{
return [self initWithFrame:CGRectZero];
@@ -274,7 +274,7 @@ - (void)layoutSubviews{
CHGridIndexRange tileRange = [layout rangeOfVisibleIndexesForContentOffset:contentOffsetY andHeight:b.size.height];
[self loadVisibleTilesForIndexPathRange:tileRange];
- //if([gridDelegate respondsToSelector:@selector(visibleTilesChangedTo:)]) [gridDelegate visibleTilesChangedTo:visibleTiles.count];
+ if([gridDelegate respondsToSelector:@selector(visibleTilesChangedTo:)]) [gridDelegate visibleTilesChangedTo:visibleTiles.count];
if(sections > 1){
CHSectionRange sectionRange = [layout sectionRangeForContentOffset:contentOffsetY andHeight:b.size.height];
@@ -345,6 +345,13 @@ - (CHGridIndexPath)indexPathForPoint:(CGPoint)point{
return CHGridIndexPathMake(0, 0);
}
+#pragma mark tile scrolling methods
+
+- (void)scrollToTileAtIndexPath:(CHGridIndexPath)indexPath animated:(BOOL)animated{
+ CGRect r = [layout tileFrameForIndexPath:indexPath];
+ [self scrollRectToVisible:r animated:animated];
+}
+
#pragma mark selection methods
- (void)deselectTileAtIndexPath:(CHGridIndexPath)indexPath{
View
2 README.markdown
@@ -24,8 +24,6 @@ NOTICE: **CHGridView is not production-level software yet. Don't use it in shipp
###Usage:
-[Download a sample view controller class](http://cameron.io/files/CHGridViewController.zip).
-
Exactly like UITableView. Just implement the two required data source methods: `numberOfTilesInSection` and `tileForIndexPath`. CHGridView assumes there is at least one section. The method `tileForIndexPath` works very much like UITableView; CHGridView reuses tiles just like UITableView reuses cells. Call `dequeueReusableTileWithIdentifier` to get a reusable tile, if it's `nil`, `init` and `autorelease` a new tile and return it.
There's two basic styles to use in GHGridView, one that resembles the Photos application, and one that mimics iPhoto and the iPad photo grid. The property that controls it is called `dynamicallyResizeTilesToFillSpace`. Set it to `YES` for the Photos app style.

0 comments on commit e459685

Please sign in to comment.