Skip to content

Commit

Permalink
Support autorotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
René Sprotte committed Apr 4, 2011
1 parent f1b3b6d commit 1c440c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions MMGridView.xcodeproj/project.pbxproj
Expand Up @@ -417,6 +417,7 @@
7E6EF3E41340B52E00DD91EC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
32 changes: 24 additions & 8 deletions MMGridView/Classes/MMGridView.m
Expand Up @@ -55,8 +55,11 @@ - (id)initWithCoder:(NSCoder *)aDecoder

- (void)createSubviews
{
self.numberOfRows = 2;
self.numberOfColumns = 3;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.contentMode = UIViewContentModeRedraw;

self.numberOfRows = 3;
self.numberOfColumns = 2;

self.backgroundColor = [UIColor clearColor];

Expand Down Expand Up @@ -98,6 +101,7 @@ - (void)setNumberOfRows:(NSUInteger)value
- (void)layoutSubviews
{
self.scrollView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
[self reloadData];
}


Expand All @@ -108,24 +112,36 @@ - (void)reloadData
}

if (self.dataSource) {
NSInteger cellMargin = 0;
NSInteger cellsPerPage = self.numberOfColumns * self.numberOfRows;
NSInteger cellMargin = 3;
NSInteger noOfCols = self.numberOfColumns;
NSInteger noOfRows = self.numberOfRows;
NSInteger cellsPerPage = noOfCols * noOfRows;

BOOL isLandscape = UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation]);
if (isLandscape) {
// In landscape mode switch rows and columns
noOfCols = self.numberOfRows;
noOfRows = self.numberOfColumns;
}

NSInteger numberOfCells = [self.dataSource numberOfCellsInGridView:self];
NSInteger numberOfPages = (int)(ceil((float)numberOfCells / (float)cellsPerPage));

CGRect gridBounds = self.scrollView.bounds;
CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)self.numberOfColumns, gridBounds.size.height / (float)self.numberOfRows);
CGRect cellBounds = CGRectMake(0, 0, gridBounds.size.width / (float)noOfCols,
gridBounds.size.height / (float)noOfRows);

[self.scrollView setContentSize:CGSizeMake((numberOfPages * gridBounds.size.width), gridBounds.size.height)];
CGSize contentSize = CGSizeMake(numberOfPages * gridBounds.size.width, gridBounds.size.height);
[self.scrollView setContentSize:contentSize];

for (NSInteger i = 0; i < numberOfCells; i++) {
MMGridViewCell *cell = [self.dataSource gridView:self cellAtIndex:i];

NSInteger page = (int)floor((float)i / (float)cellsPerPage);
NSInteger row = (int)floor((float)i / (float)self.numberOfColumns) - (page * self.numberOfRows);
NSInteger row = (int)floor((float)i / (float)noOfCols) - (page * noOfRows);

CGPoint origin = CGPointMake((page * gridBounds.size.width) + ((i % self.numberOfColumns) * cellBounds.size.width), (row * cellBounds.size.height));
CGPoint origin = CGPointMake((page * gridBounds.size.width) + ((i % noOfCols) * cellBounds.size.width),
(row * cellBounds.size.height));

CGRect f = CGRectMake(origin.x, origin.y, cellBounds.size.width, cellBounds.size.height);
cell.frame = CGRectInset(f, cellMargin, cellMargin);
Expand Down
12 changes: 12 additions & 0 deletions MMGridViewDemo/RootViewController.m
Expand Up @@ -10,6 +10,10 @@

@implementation RootViewController

// ----------------------------------------------------------------------------------

#pragma - Object lifecycle

- (void)dealloc
{
[gridView release];
Expand All @@ -32,6 +36,14 @@ - (void)loadView
[self.view addSubview:gridView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

// ----------------------------------------------------------------------------------

#pragma - MMGridViewDataSource
Expand Down

0 comments on commit 1c440c1

Please sign in to comment.