Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle bounds change on height change #167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Classes/AQGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
Expand Down Expand Up @@ -545,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];
}

Expand Down