Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GMGridViewDidScroll, Geometry and pageForContentOffset #22

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ xcuserdata/
*.xcodeproj/xcuserdata/
*.xcodeproj/project.xcworkspace/xcuserdata/
*.xcuserstate
build/
9 changes: 9 additions & 0 deletions GMGridView/API/GMGridView.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ typedef enum
- (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2;
- (void)scrollToObjectAtIndex:(NSInteger)index animated:(BOOL)animated;

// Geometry
// converts a point, taking into account the internal scroll view
- (CGPoint) convertScrolledPoint:(CGPoint)point toView:(UIView*)view;
// converts a rect, taking into account the internal scroll position
- (CGRect) convertScrolledRect:(CGRect)rect toView:(UIView*)view;

@end


Expand Down Expand Up @@ -125,6 +131,9 @@ typedef enum
@required
- (void)GMGridView:(GMGridView *)gridView didTapOnItemAtIndex:(NSInteger)position;

@optional
// tells the delegate that the scroll view just did scroll. similar in concept to [UIScrollView scrollViewDidScroll:]
- (void)GMGridViewDidScroll:(GMGridView*)gridView;
@end


Expand Down
16 changes: 14 additions & 2 deletions GMGridView/API/GMGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ - (BOOL)showsHorizontalScrollIndicator
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self loadRequiredItems];
if ([self.actionDelegate respondsToSelector:@selector(GMGridViewDidScroll:)]) {
[self.actionDelegate GMGridViewDidScroll:self];
}
}

//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1034,7 +1037,7 @@ - (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture
{
CGPoint locationTouch = [_tapGesture locationInView:_scrollView];
NSInteger position = [self.layoutStrategy itemPositionFromLocation:locationTouch];

if (position != GMGV_INVALID_POSITION)
{
[self.actionDelegate GMGridView:self didTapOnItemAtIndex:position];
Expand Down Expand Up @@ -1533,5 +1536,14 @@ - (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2
];
}


//////////////////////////////////////////////////////////////
// Geometry
//////////////////////////////////////////////////////////////
- (CGPoint) convertScrolledPoint:(CGPoint)point toView:(UIView*)view
{
return [_scrollView convertPoint:point toView:view];
}
- (CGRect) convertScrolledRect:(CGRect)rect toView:(UIView*)view
{
return [_scrollView convertRect:rect toView:view];}
@end
2 changes: 1 addition & 1 deletion GMGridView/API/GMGridViewLayoutStrategies.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ typedef enum {
- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page;
- (NSInteger)columnForItemAtPosition:(NSInteger)position;
- (NSInteger)rowForItemAtPosition:(NSInteger)position;

- (NSUInteger) pageForContentOffset:(CGPoint)offset;
@end


Expand Down
19 changes: 6 additions & 13 deletions GMGridView/API/GMGridViewLayoutStrategies.m
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,7 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
bounds.size.width - self.minEdgeInsets.right - self.minEdgeInsets.left,
bounds.size.height - self.minEdgeInsets.top - self.minEdgeInsets.bottom);

_numberOfItemsPerColumn = 1;

while ((_numberOfItemsPerColumn + 1) * (self.itemSize.height + self.itemSpacing) - self.itemSpacing <= actualBounds.size.height)
{
_numberOfItemsPerColumn++;
}
_numberOfItemsPerColumn = floor((actualBounds.size.height + self.itemSpacing) / (self.itemSize.height + self.itemSpacing));

NSInteger numberOfColumns = ceil(self.itemCount / (1.0 * self.numberOfItemsPerColumn));

Expand Down Expand Up @@ -372,14 +367,9 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
{
[super rebaseWithItemCount:count insideOfBounds:bounds];

_numberOfItemsPerRow = 1;

NSInteger gridContentMaxWidth = self.gridBounds.size.width - self.minEdgeInsets.right - self.minEdgeInsets.left;

while ((self.numberOfItemsPerRow + 1) * (self.itemSize.width + self.itemSpacing) - self.itemSpacing <= gridContentMaxWidth)
{
_numberOfItemsPerRow++;
}
_numberOfItemsPerRow = floor((gridContentMaxWidth + self.itemSpacing) / (self.itemSize.width + self.itemSpacing));

_numberOfItemsPerPage = _numberOfItemsPerRow * _numberOfItemsPerColumn;
_numberOfPages = ceil(self.itemCount * 1.0 / self.numberOfItemsPerPage);
Expand Down Expand Up @@ -511,6 +501,10 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
return NSMakeRange(firstPosition, (lastPosition - firstPosition));
}

- (NSUInteger) pageForContentOffset:(CGPoint)offset
{
return floor(offset.x / self.gridBounds.size.width);
}
@end


Expand Down Expand Up @@ -571,4 +565,3 @@ - (NSInteger)rowForItemAtPosition:(NSInteger)position
}

@end