diff --git a/.gitignore b/.gitignore index b154df8..6b07d24 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ xcuserdata/ *.xcodeproj/xcuserdata/ *.xcodeproj/project.xcworkspace/xcuserdata/ *.xcuserstate +build/ diff --git a/GMGridView/API/GMGridView.h b/GMGridView/API/GMGridView.h index 5311d54..9e4d717 100644 --- a/GMGridView/API/GMGridView.h +++ b/GMGridView/API/GMGridView.h @@ -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 @@ -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 diff --git a/GMGridView/API/GMGridView.m b/GMGridView/API/GMGridView.m index 2d4a78a..a02d3a1 100644 --- a/GMGridView/API/GMGridView.m +++ b/GMGridView/API/GMGridView.m @@ -390,6 +390,9 @@ - (BOOL)showsHorizontalScrollIndicator - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self loadRequiredItems]; + if ([self.actionDelegate respondsToSelector:@selector(GMGridViewDidScroll:)]) { + [self.actionDelegate GMGridViewDidScroll:self]; + } } ////////////////////////////////////////////////////////////// @@ -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]; @@ -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 diff --git a/GMGridView/API/GMGridViewLayoutStrategies.h b/GMGridView/API/GMGridViewLayoutStrategies.h index 5b816b0..0ee5ee8 100644 --- a/GMGridView/API/GMGridViewLayoutStrategies.h +++ b/GMGridView/API/GMGridViewLayoutStrategies.h @@ -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 diff --git a/GMGridView/API/GMGridViewLayoutStrategies.m b/GMGridView/API/GMGridViewLayoutStrategies.m index 1a39dbc..da84164 100644 --- a/GMGridView/API/GMGridViewLayoutStrategies.m +++ b/GMGridView/API/GMGridViewLayoutStrategies.m @@ -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)); @@ -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); @@ -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 @@ -571,4 +565,3 @@ - (NSInteger)rowForItemAtPosition:(NSInteger)position } @end -