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

Issue #96:Fix for Buggy horizontal mode #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Classes/AQGridView.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
UIView * _headerView;
UIView * _footerView;


struct
{
unsigned resizesCellWidths:1;
Expand All @@ -138,7 +139,10 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
unsigned allowsSelection:1;
unsigned backgroundViewExtendsUp:1;
unsigned backgroundViewExtendsDown:1;
unsigned backgroundViewExtendsLeft:1;
unsigned backgroundViewExtendsRight:1;
unsigned usesPagedHorizontalScrolling:1;
unsigned layoutChange:1;
unsigned updating:1; // unused
unsigned ignoreTouchSelect:1;
unsigned needsReload:1;
Expand Down Expand Up @@ -230,7 +234,10 @@ extern NSString * const AQGridViewSelectionDidChangeNotification;
@property (nonatomic, retain) UIView * backgroundView; // specifies a view to place behind the cells
@property (nonatomic) BOOL backgroundViewExtendsUp; // default is NO. If YES, the background view extends upward and is visible during a bounce.
@property (nonatomic) BOOL backgroundViewExtendsDown; // default is NO. If YES, the background view extends downward and is visible during a bounce.
@property (nonatomic) BOOL backgroundViewExtendsLeft; // default is NO. If YES, the background view extends left and is visible during a bounce.
@property (nonatomic) BOOL backgroundViewExtendsRight; // default is NO. If YES, the background view extends right and is visible during a bounce.
@property (nonatomic) BOOL usesPagedHorizontalScrolling; // default is NO, and scrolls verticalls only. Set to YES to have horizontal-only scrolling by page.
@property (nonatomic) BOOL layoutChange;

@property (nonatomic) AQGridViewCellSeparatorStyle separatorStyle; // default is AQGridViewCellSeparatorStyleEmptySpace
@property (nonatomic, retain) UIColor * separatorColor; // ignored unless separatorStyle == AQGridViewCellSeparatorStyleSingleLine. Default is standard separator gray.
Expand Down
168 changes: 134 additions & 34 deletions Classes/AQGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ - (void) _sharedGridViewInit
self.canCancelContentTouches = YES;

_selectedIndex = NSNotFound;
_flags.layoutChange = NO;
_pendingSelectionIndex = NSNotFound;

_flags.resizesCellWidths = 0;
Expand All @@ -120,6 +121,7 @@ - (void) _sharedGridViewInit
_flags.allowsSelection = 1;
_flags.usesPagedHorizontalScrolling = NO;
_flags.contentSizeFillsBounds = 1;

}

- (id)initWithFrame: (CGRect) frame
Expand Down Expand Up @@ -200,6 +202,8 @@ - (AQGridViewLayoutDirection) layoutDirection
- (void) setLayoutDirection: (AQGridViewLayoutDirection) direction
{
_gridData.layoutDirection = direction;
_flags.layoutChange = YES;

}

- (NSUInteger) numberOfItems
Expand Down Expand Up @@ -249,6 +253,27 @@ - (void) setBackgroundViewExtendsUp: (BOOL) value
_flags.backgroundViewExtendsUp = (value ? 1 : 0);
}

- (BOOL) backgroundViewExtendsLeft
{
return ( _flags.backgroundViewExtendsLeft);
}

- (void) setBackgroundViewExtendsLeft: (BOOL) value
{
_flags.backgroundViewExtendsLeft = (value ? 1 : 0);
}


- (BOOL) backgroundViewExtendsRight
{
return ( _flags.backgroundViewExtendsRight);
}

- (void) setBackgroundViewExtendsRight: (BOOL) value
{
_flags.backgroundViewExtendsRight = (value ? 1 : 0);
}

- (BOOL) requiresSelection
{
return ( _flags.requiresSelection );
Expand All @@ -259,6 +284,16 @@ - (void) setRequiresSelection: (BOOL) value
_flags.requiresSelection = (value ? 1 : 0);
}

- (BOOL) layoutChange
{
return ( _flags.layoutChange );
}

- (void) setLayoutChange: (BOOL) value
{
_flags.layoutChange = (value ? 1 : 0);
}

- (BOOL) resizesCellWidthToFit
{
return ( _flags.resizesCellWidths );
Expand Down Expand Up @@ -510,6 +545,8 @@ - (void) setContentSize: (CGSize) newSize
{
if ( (_flags.contentSizeFillsBounds == 1) && (newSize.height < self.bounds.size.height) )
newSize.height = self.bounds.size.height;
if ( (_flags.contentSizeFillsBounds == 1) && (newSize.width < self.bounds.size.width) )
newSize.width = self.bounds.size.width;

if (self.gridFooterView)
{
Expand All @@ -522,9 +559,12 @@ - (void) setContentSize: (CGSize) newSize
if (newSize.height < footerHeight + minimumHeight)
newSize.height = minimumHeight;
}

newSize.height = fmax(newSize.height, self.frame.size.height+1);

if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
newSize.height = fmax(newSize.height, self.frame.size.height+1);
}else{
newSize.width = fmax(newSize.width, self.frame.size.width+1);

}
CGSize oldSize = self.contentSize;
[super setContentSize: newSize];

Expand Down Expand Up @@ -558,6 +598,10 @@ - (void) setBounds: (CGRect) bounds

if ( !CGSizeEqualToSize(bounds.size, oldBounds.size) )
[self handleGridViewBoundsChanged: oldBounds toNewBounds: bounds];
if ( _flags.layoutChange == YES ){
[self handleGridViewBoundsChanged: oldBounds toNewBounds: bounds];
_flags.layoutChange = NO;
}
}

- (BOOL) isEditing
Expand Down Expand Up @@ -667,40 +711,77 @@ - (void) layoutSubviews
}

CGRect rect = CGRectZero;
rect.size.width = self.bounds.size.width;
rect.size.height = self.contentSize.height - (_gridData.topPadding + _gridData.bottomPadding);
rect.origin.y += _gridData.topPadding;


if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
rect.size.width = self.bounds.size.width;
rect.size.height = self.contentSize.height - (_gridData.topPadding + _gridData.bottomPadding);
rect.origin.y += _gridData.topPadding;
}else{
rect.size.height = self.bounds.size.height;
rect.size.width = self.contentSize.width - (_gridData.leftPadding + _gridData.rightPadding);
rect.origin.x += _gridData.leftPadding;
}
// Make sure background is an integral number of rows tall. That way, it draws patterned colours correctly on all OSes.
CGRect backgroundRect = rect;

if ([self backgroundViewExtendsUp]) {
backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
backgroundRect.size.height += MAX_BOUNCE_DISTANCE; // don't just move it, grow it
}

if ([self backgroundViewExtendsDown]) {
backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
}



CGFloat minimumHeight = rect.size.height,
actualHeight = 0;
minimumWidth = rect.size.width,
actualWidth = 0,
actualHeight = 0;
if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
if (([_gridData numberOfItems] == 0) || ([_gridData numberOfItemsPerRow] == 0)) {

if (([_gridData numberOfItems] == 0) || ([_gridData numberOfItemsPerRow] == 0)) {
actualHeight = [_gridData cellSize].height;

actualHeight = [_gridData cellSize].height;
} else {

} else {
actualHeight = [_gridData cellSize].height * (ceilf((CGFloat)[_gridData numberOfItems] / (CGFloat)[_gridData numberOfItemsPerRow]) + 1);

actualHeight = [_gridData cellSize].height * ([_gridData numberOfItems] / [_gridData numberOfItemsPerRow] + 1);
}
for (; actualHeight < minimumHeight; actualHeight += [_gridData cellSize].height) {
}
backgroundRect.size.height = actualHeight;
}else{
if (([_gridData numberOfItems] == 0) || ([_gridData numberOfItemsPerColumn] == 0)) {

actualWidth = [_gridData cellSize].width;

} else {

actualWidth = [_gridData cellSize].width * (ceilf((CGFloat)[_gridData numberOfItems] / (CGFloat)[_gridData numberOfItemsPerColumn]) + 1);

}
for (; actualHeight < minimumHeight; actualHeight += [_gridData cellSize].height) {
}
backgroundRect.size.height = actualHeight;
}
for (; actualWidth < minimumWidth; actualWidth += [_gridData cellSize].width) {
}
backgroundRect.size.width = actualWidth;

}

if(self.layoutDirection == AQGridViewLayoutDirectionVertical){

if ([self backgroundViewExtendsUp]) {
backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
backgroundRect.size.height += MAX_BOUNCE_DISTANCE; // don't just move it, grow it

}

if ([self backgroundViewExtendsDown]) {
backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
}
}
else{
if ([self backgroundViewExtendsLeft]) {
backgroundRect.origin.x = backgroundRect.origin.x - MAX_BOUNCE_DISTANCE;
backgroundRect.size.width += MAX_BOUNCE_DISTANCE; // don't just move it, grow it
}

if ([self backgroundViewExtendsRight]) {
backgroundRect.size.width = backgroundRect.size.width + MAX_BOUNCE_DISTANCE;
}
}

self.backgroundView.frame = backgroundRect;
self.backgroundView.frame = backgroundRect;

if ( _headerView != nil )
{
Expand All @@ -719,6 +800,8 @@ - (void) layoutSubviews
_footerView.frame = rect;
[self bringSubviewToFront:_footerView];
}


}

- (CGRect) rectForItemAtIndex: (NSUInteger) index
Expand Down Expand Up @@ -874,7 +957,12 @@ - (void) fixCellsFromAnimation

//NSAssert([newVisibleCells count] == _visibleIndices.length, @"visible cell count after animation doesn't match visible indices");

[newVisibleCells sortUsingSelector: @selector(compareOriginAgainstCell:)];
if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
[newVisibleCells sortUsingSelector: @selector(compareOriginAgainstCellVertical:)];
}else
{
[newVisibleCells sortUsingSelector: @selector(compareOriginAgainstCellHorizontal:)];
}
[_visibleCells removeObjectsInArray: newVisibleCells];
[_visibleCells makeObjectsPerformSelector: @selector(removeFromSuperview)];
[_visibleCells setArray: newVisibleCells];
Expand Down Expand Up @@ -1190,14 +1278,26 @@ - (void) setBackgroundView: (UIView *) newView

CGRect backgroundRect = CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height);

if ([self backgroundViewExtendsUp]) {
backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
backgroundRect.size.height += MAX_BOUNCE_DISTANCE; // don't just move it, grow it
}
if(self.layoutDirection == AQGridViewLayoutDirectionVertical){
if ([self backgroundViewExtendsUp]) {
backgroundRect.origin.y = backgroundRect.origin.y - MAX_BOUNCE_DISTANCE;
backgroundRect.size.height += MAX_BOUNCE_DISTANCE; // don't just move it, grow it
}

if ([self backgroundViewExtendsDown]) {
backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
if ([self backgroundViewExtendsDown]) {
backgroundRect.size.height = backgroundRect.size.height + MAX_BOUNCE_DISTANCE;
}
}else{

if ([self backgroundViewExtendsLeft]) {
backgroundRect.origin.x = backgroundRect.origin.x - MAX_BOUNCE_DISTANCE;
backgroundRect.size.width += MAX_BOUNCE_DISTANCE; // don't just move it, grow it
}

if ([self backgroundViewExtendsRight]) {
backgroundRect.size.width = backgroundRect.size.width + MAX_BOUNCE_DISTANCE;
}
}

_backgroundView.frame = backgroundRect;

Expand Down
4 changes: 2 additions & 2 deletions Classes/AQGridViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ typedef enum {
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// Sorting
- (NSComparisonResult) compareOriginAgainstCell: (AQGridViewCell *) otherCell;

- (NSComparisonResult) compareOriginAgainstCellVertical: (AQGridViewCell *) otherCell;
- (NSComparisonResult) compareOriginAgainstCellHorizontal: (AQGridViewCell *) otherCell;
@end
22 changes: 21 additions & 1 deletion Classes/AQGridViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ - (void) dealloc
CFRelease( _selectionColorInfo );
}

- (NSComparisonResult) compareOriginAgainstCell: (AQGridViewCell *) otherCell

- (NSComparisonResult) compareOriginAgainstCellVertical: (AQGridViewCell *) otherCell
{
CGPoint myOrigin = self.frame.origin;
CGPoint theirOrigin = otherCell.frame.origin;
Expand All @@ -118,6 +119,25 @@ - (NSComparisonResult) compareOriginAgainstCell: (AQGridViewCell *) otherCell
return ( NSOrderedSame );
}

- (NSComparisonResult) compareOriginAgainstCellHorizontal: (AQGridViewCell *) otherCell
{
CGPoint myOrigin = self.frame.origin;
CGPoint theirOrigin = otherCell.frame.origin;

if ( myOrigin.x > theirOrigin.x )
return ( NSOrderedDescending );
else if ( myOrigin.x < theirOrigin.x )
return ( NSOrderedAscending );

if ( myOrigin.y > theirOrigin.y )
return ( NSOrderedDescending );
else if ( myOrigin.y < theirOrigin.y )
return ( NSOrderedAscending );

return ( NSOrderedSame );
}


- (UIView *) contentView
{
if ( _contentView == nil )
Expand Down
2 changes: 2 additions & 0 deletions Classes/AQGridViewData.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
- (CGRect) rectForEntireGrid;
- (CGSize) sizeForEntireGrid;
- (NSUInteger) numberOfItemsPerRow;
- (NSUInteger) numberOfItemsPerColumn;


- (CGRect) cellRectAtIndex: (NSUInteger) index;
- (CGRect) cellRectForPoint: (CGPoint) point;
Expand Down