Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add GMGridViewDidScroll, Geometry and pageForContentOffset #22

Open
wants to merge 14 commits into from

2 participants

@rantav

After pulling the latest from gmoledina/GMGridView I added a few small features that I needed to complete the picture for invi. I think they can be useful in a broader scope.
Please note that the list of commits (12) looks scarry, but most of them are just leftovers from previously non-pulled commits. There are only two interesting commits,
rantav@8354e20
and
rantav@664af22

rantav added some commits
@rantav rantav Add pagingEnabled to the grid. It simply delegates the property back …
…to the undelying scrollView
b128ad4
@rantav rantav Fix name of isPagingEnabled to pagingEnabled. Old java habits... d58ccf1
@rantav rantav Implement GMGridViewLayoutHorizontalPagedLtr. a layout for paged hori…
…zontal scroll where items in each page flow from top-left to bottom-right
abf5a73
@rantav rantav Add convertScrolledPoint and convertScrolledRect so users of GMGridVi…
…ew are able to get the position of a scrolled element
35b4bda
@rantav rantav Add 'build' to gitignore 53f7a0e
@rantav rantav Add support for better programatic scroll control.
Expose the contentOffset
Allow scrolling to a particular point (and not only to a particular item)
Add more page queries to the GMGridViewLayoutHorizontalPagedLtrStrategy
... it's all part of the same scheme...
92aa779
@rantav rantav Add GMGridViewDidScroll so that users know when GMScrollView is scrol…
…ling
a6d3057
@gmoledina
Owner

Looks good ! I'll check it tomorrow and merge it.

@gmoledina
Owner

Scrolling to a particular point will be weird when paging is enabled

OK, this commit has two different things even though in my mind they are related.
We have the two geometry methods:

  • (CGPoint)originForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page;
  • (CGPoint)originForPage:(NSUInteger)page;

They are useful to know the physical location on the screen a certain item in a page (I use it to 'fly' things away from the grid off to another UIView in an animation when the user touches them)

The second thing is

  • (void)setContentOffset:(CGPoint)offset animated:(BOOL)animated;

Well, I have a paging control that I use to scroll b/w pages. So I use a combination of originForPage and setContentOffset.
I see why this breaks encapsulation and I'm open for suggestions... the goal is to allow an external page control to slide switch pages in the grid.

As a matter of fact when I come to think of it, regarding setContentOffset and pagingEnabled - this is consistent with how UIScrollView behaves. Event in UIScrollView it's possible to programmatically scroll to any point in the scroll view even though the scroll view is paged. So although I agree with you that this is breaking encapsulation to some degree, at least we didn't break it first and we're consistent with apple model ;)

@gmoledina
Owner

I agree.
I'll add this on my next update when I'll add an API to allow having a pageControl (so knowing when the page changed).

@gmoledina
Owner

Is it better to have just one property that controls both horizontal and vertical scroll indicator ?
As we never have both of them visible at the same time anyway.

Alright! I've pulled your code on my local repo. It will be part of the next release.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 16, 2011
  1. @rantav

    Add pagingEnabled to the grid. It simply delegates the property back …

    rantav authored
    …to the undelying scrollView
  2. @rantav
  3. @rantav

    Implement GMGridViewLayoutHorizontalPagedLtr. a layout for paged hori…

    rantav authored
    …zontal scroll where items in each page flow from top-left to bottom-right
  4. @rantav

    Add convertScrolledPoint and convertScrolledRect so users of GMGridVi…

    rantav authored
    …ew are able to get the position of a scrolled element
  5. @rantav

    Add 'build' to gitignore

    rantav authored
  6. @rantav

    Add support for better programatic scroll control.

    rantav authored
    Expose the contentOffset
    Allow scrolling to a particular point (and not only to a particular item)
    Add more page queries to the GMGridViewLayoutHorizontalPagedLtrStrategy
    ... it's all part of the same scheme...
  7. @rantav
Commits on Dec 7, 2011
  1. @rantav
Commits on Dec 23, 2011
  1. @rantav
Commits on Dec 24, 2011
  1. @rantav

    1. Add support for optional GMGridViewActionDelegate selector GMGridV…

    rantav authored
    …iewDidScroll:
    
    2. Add Geometry selectors convertScrolledPoint:toView: and convertScrolledRect:rect toView:
  2. @rantav
  3. @rantav

    Merge with rantav/master

    rantav authored
Commits on Jan 13, 2012
  1. @rantav
  2. @rantav
This page is out of date. Refresh to see the latest.
View
1  .gitignore
@@ -6,3 +6,4 @@ xcuserdata/
*.xcodeproj/xcuserdata/
*.xcodeproj/project.xcworkspace/xcuserdata/
*.xcuserstate
+build/
View
9 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
View
16 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
View
2  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
View
19 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
-
Something went wrong with that request. Please try again.