From ff2833c96800b30bfe4d660ee4e2355226d9353d Mon Sep 17 00:00:00 2001 From: Walter Hatcher Date: Tue, 28 Aug 2012 14:52:44 -0400 Subject: [PATCH 1/2] Recalculate contentOffset for device rotation --- Classes/AQGridView.m | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Classes/AQGridView.m b/Classes/AQGridView.m index 3f62735..c496d36 100755 --- a/Classes/AQGridView.m +++ b/Classes/AQGridView.m @@ -459,9 +459,16 @@ - (void) updateContentRectWithOldMaxLocation: (CGPoint) oldMaxLocation gridSize: - (void) handleGridViewBoundsChanged: (CGRect) oldBounds toNewBounds: (CGRect) bounds { + CGPoint oldOffset = self.contentOffset; CGSize oldGridSize = [_gridData sizeForEntireGrid]; BOOL wasAtBottom = ((oldGridSize.height != 0.0) && (CGRectGetMaxY(oldBounds) == oldGridSize.height)); + // cell height may change on rotation + if ( _flags.dataSourceGridCellSize == 1 ) + { + [_gridData setDesiredCellSize: [_dataSource portraitGridCellSizeForGridView: self]]; + } + [_gridData gridViewDidChangeBoundsSize: bounds.size]; _flags.numColumns = [_gridData numberOfItemsPerRow]; CGSize newGridSize = [_gridData sizeForEntireGrid]; @@ -478,6 +485,19 @@ - (void) handleGridViewBoundsChanged: (CGRect) oldBounds toNewBounds: (CGRect) b self.contentOffset = contentRect.origin; } } + else if (!wasAtBottom && !CGPointEqualToPoint(oldBounds.origin, CGPointZero) && !CGSizeEqualToSize(oldGridSize, CGSizeZero)) + { + // If not at the bottom or top (they are handled above and in updateContentRectWithOldMaxLocation) + // and we have data to work with (sometimes we can start with the oldSize == 0), + // constentOffset.y needs to be reset to be proportional to the new height. Then snap it to the nearest cell boundry. + + CGFloat proportionalY = newGridSize.height * (oldOffset.y / oldGridSize.height); + CGRect cellRect = [_gridData cellRectForPoint:CGPointMake(self.contentOffset.x, proportionalY)]; + CGFloat newY = (proportionalY < (cellRect.origin.y + (cellRect.size.height / 2.0f))) ? + cellRect.origin.y : cellRect.origin.y + cellRect.size.height; + self.contentOffset = CGPointMake(self.contentOffset.x, newY); + } + [self updateVisibleGridCellsNow]; _flags.allCellsNeedLayout = 1; From 1c28786352e0bf4867f98a58cd08f5f04369e6ff Mon Sep 17 00:00:00 2001 From: Walter Hatcher Date: Thu, 27 Sep 2012 10:00:07 -0400 Subject: [PATCH 2/2] Handle bounds change when height changes --- Classes/AQGridView.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/AQGridView.m b/Classes/AQGridView.m index c496d36..996a2ff 100755 --- a/Classes/AQGridView.m +++ b/Classes/AQGridView.m @@ -565,7 +565,7 @@ - (void) setFrame: (CGRect) newFrame [super setFrame: newFrame]; CGRect newBounds = self.bounds; - if ( newBounds.size.width != oldBounds.size.width ) + if ( !CGSizeEqualToSize(newBounds.size, oldBounds.size) ) [self handleGridViewBoundsChanged: oldBounds toNewBounds: newBounds]; }