Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add property to control layout direction #25

Merged
merged 1 commit into from

2 participants

@mosamer

Added a property to control layout direction. It can be MMGridViewLayoutDirctionHorizontal which is the default one and will layout pages beside each other horizontally. Another value is MMGridViewLayoutDirctionVertical which will layout pages below each other vertically.

@renspr renspr merged commit 05da985 into provideal:master
@renspr
Collaborator

Hi. Nice addition. Thanks.

Would be nice if you update the demo app to show the feature.

René

@mosamer mosamer referenced this pull request from a commit in mosamer/MMGridView
@mosamer mosamer Issue #25: Modified demo project to show the layout direction effect 6b9ba47
@renspr
Collaborator

Hi,

the demo now shows a 2x2 grid instead of a 2x3 grid with space left on the bottom. Please fix Issue #27, or I need to revert the pull request, as I have no time to look into it now.

Beste regards,

René

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 27, 2012
  1. @mosamer
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 10 deletions.
  1. +6 −0 MMGridView/Classes/MMGridView.h
  2. +33 −10 MMGridView/Classes/MMGridView.m
View
6 MMGridView/Classes/MMGridView.h
@@ -25,6 +25,11 @@
@class MMGridView;
+typedef enum
+{
+ MMGridViewLayoutDirectionHorizontal,
+ MMGridViewLayoutDirectionVertical
+} MMGridViewLayoutDirection;
// ----------------------------------------------------------------------------------
#pragma - MMGridViewDataSource
@@ -67,6 +72,7 @@
@property (nonatomic, readonly) NSUInteger currentPageIndex;
@property (nonatomic, readonly) NSUInteger numberOfPages;
@property (nonatomic, assign, getter = isPagingEnabled) BOOL pagingEnabled;
+@property (nonatomic, assign) MMGridViewLayoutDirection layoutDirection;
- (void)reloadData;
@end
View
43 MMGridView/Classes/MMGridView.m
@@ -72,6 +72,7 @@ - (void)createSubviews
numberOfRows = 3;
numberOfColumns = 2;
currentPageIndex = 0;
+ self.layoutDirection = MMGridViewLayoutDirectionHorizontal;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.contentMode = UIViewContentModeRedraw;
@@ -110,27 +111,42 @@ - (void)drawRect:(CGRect)rect
CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols,
gridBounds.size.height / (float)noOfRows);
- CGSize contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
+ CGSize contentSize = CGSizeZero;
+ if(self.layoutDirection == MMGridViewLayoutDirectionHorizontal)
+ {
+ contentSize = CGSizeMake(self.numberOfPages * gridBounds.size.width, gridBounds.size.height);
+ }else
+ {
+ contentSize = CGSizeMake(gridBounds.size.width, self.numberOfPages * gridBounds.size.height);
+ }
[self.scrollView setContentSize:contentSize];
for (UIView *v in self.scrollView.subviews) {
[v removeFromSuperview];
}
-
+
for (NSInteger i = 0; i < [self.dataSource numberOfCellsInGridView:self]; i++) {
MMGridViewCell *cell = [self.dataSource gridView:self cellAtIndex:i];
[cell performSelector:@selector(setGridView:) withObject:self];
[cell performSelector:@selector(setIndex:) withObject:[NSNumber numberWithInt:i]];
-
+
NSInteger page = (int)floor((float)i / (float)cellsPerPage);
- NSInteger row = (int)floor((float)i / (float)noOfCols) - (page * noOfRows);
-
- CGPoint origin = CGPointMake((page * gridBounds.size.width) + ((i % noOfCols) * cellBounds.size.width),
- (row * cellBounds.size.height));
-
+ CGPoint origin = CGPointZero;
+ NSInteger indexInPage = i % cellsPerPage;
+ NSInteger col = indexInPage % noOfRows;
+ NSInteger row = (NSInteger)floorf((float)indexInPage / (float)noOfRows);
+ if(self.layoutDirection == MMGridViewLayoutDirectionHorizontal)
+ {
+ origin = CGPointMake(page * gridBounds.size.width, 0.0);
+ }else
+ {
+ origin = CGPointMake(0.0, page * gridBounds.size.height);
+ }
+ origin.x += col * cellBounds.size.width;
+ origin.y += row * cellBounds.size.height;
+
CGRect f = CGRectMake(origin.x, origin.y, cellBounds.size.width, cellBounds.size.height);
cell.frame = CGRectInset(f, self.cellMargin, self.cellMargin);
-
[self.scrollView addSubview:cell];
}
}
@@ -197,10 +213,17 @@ - (void)cellWasDoubleTapped:(MMGridViewCell *)cell
- (void)updateCurrentPageIndex
{
+ if(self.layoutDirection == MMGridViewLayoutDirectionHorizontal)
+ {
CGFloat pageWidth = scrollView.frame.size.width;
NSUInteger cpi = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.currentPageIndex = cpi;
-
+ }else
+ {
+ CGFloat pageHeight = scrollView.frame.size.height;
+ NSUInteger cpi = floor((scrollView.contentOffset.y - pageHeight /2) / pageHeight) + 1;
+ self.currentPageIndex = cpi;
+ }
if (delegate && [delegate respondsToSelector:@selector(gridView:changedPageToIndex:)]) {
[self.delegate gridView:self changedPageToIndex:self.currentPageIndex];
}
Something went wrong with that request. Please try again.