Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion GMGridView/GMGridView.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ typedef enum
// Customizing Options
@property (nonatomic, gm_weak) IBOutlet UIView *mainSuperView; // Default is self
@property (nonatomic) GMGridViewStyle style; // Default is GMGridViewStyleSwap
@property (nonatomic) NSInteger itemSpacing; // Default is 10
@property (nonatomic) NSInteger horizontalItemSpacing; // Default is 10
@property (nonatomic) NSInteger verticalItemSpacing; // Default is 10
@property (nonatomic) BOOL centerGrid; // Default is YES
@property (nonatomic) UIEdgeInsets minEdgeInsets; // Default is (5, 5, 5, 5)
@property (nonatomic) CFTimeInterval minimumPressDuration; // Default is 0.2; if set to 0, the scrollView will not be scrollable
Expand Down
18 changes: 13 additions & 5 deletions GMGridView/GMGridView.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ @implementation GMGridView
@synthesize sortingDelegate = _sortingDelegate, dataSource = _dataSource, transformDelegate = _transformDelegate, actionDelegate = _actionDelegate;
@synthesize mainSuperView = _mainSuperView;
@synthesize layoutStrategy = _layoutStrategy;
@synthesize itemSpacing = _itemSpacing;
@synthesize horizontalItemSpacing = _horizontalItemSpacing;
@synthesize verticalItemSpacing = _verticalItemSpacing;
@synthesize style = _style;
@synthesize minimumPressDuration;
@synthesize centerGrid = _centerGrid;
Expand Down Expand Up @@ -238,7 +239,8 @@ - (void)commonInit

self.mainSuperView = self;
self.editing = NO;
self.itemSpacing = 10;
self.horizontalItemSpacing = 10;
self.verticalItemSpacing = 10;
self.style = GMGridViewStyleSwap;
self.minimumPressDuration = 0.2;
self.showFullSizeViewWithAlphaWhenTransforming = YES;
Expand Down Expand Up @@ -388,9 +390,15 @@ - (void)setLayoutStrategy:(id<GMGridViewLayoutStrategy>)layoutStrategy
[self setNeedsLayout];
}

- (void)setItemSpacing:(NSInteger)itemSpacing
- (void)setHorizontalItemSpacing:(NSInteger)itemSpacing
{
_itemSpacing = itemSpacing;
_horizontalItemSpacing = itemSpacing;
[self setNeedsLayout];
}

- (void)setVerticalItemSpacing:(NSInteger)itemSpacing
{
_verticalItemSpacing = itemSpacing;
[self setNeedsLayout];
}

Expand Down Expand Up @@ -1217,7 +1225,7 @@ - (NSInteger)positionForItemSubview:(GMGridViewCell *)view

- (void)recomputeSizeAnimated:(BOOL)animated
{
[self.layoutStrategy setupItemSize:_itemSize andItemSpacing:self.itemSpacing withMinEdgeInsets:self.minEdgeInsets andCenteredGrid:self.centerGrid];
[self.layoutStrategy setupItemSize:_itemSize horizontalItemSpacing:self.horizontalItemSpacing verticalItemSpacing:self.verticalItemSpacing withMinEdgeInsets:self.minEdgeInsets andCenteredGrid:self.centerGrid];
[self.layoutStrategy rebaseWithItemCount:_numberTotalItems insideOfBounds:self.bounds];

CGSize contentSize = [self.layoutStrategy contentSize];
Expand Down
10 changes: 6 additions & 4 deletions GMGridView/GMGridViewLayoutStrategies.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ typedef enum {
- (GMGridViewLayoutStrategyType)type;

// Setup
- (void)setupItemSize:(CGSize)itemSize andItemSpacing:(NSInteger)spacing withMinEdgeInsets:(UIEdgeInsets)edgeInsets andCenteredGrid:(BOOL)centered;
- (void)setupItemSize:(CGSize)itemSize horizontalItemSpacing:(NSInteger)hSpacing verticalItemSpacing:(NSInteger)vSpacing withMinEdgeInsets:(UIEdgeInsets)edgeInsets andCenteredGrid:(BOOL)centered;

// Recomputing
- (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds;
Expand All @@ -91,7 +91,8 @@ typedef enum {

// All of these vars should be set in the setup method of the child class
CGSize _itemSize;
NSInteger _itemSpacing;
NSInteger _horizontalItemSpacing;
NSInteger _verticalItemSpacing;
UIEdgeInsets _minEdgeInsets;
BOOL _centeredGrid;

Expand All @@ -105,7 +106,8 @@ typedef enum {
@property (nonatomic, readonly) GMGridViewLayoutStrategyType type;

@property (nonatomic, readonly) CGSize itemSize;
@property (nonatomic, readonly) NSInteger itemSpacing;
@property (nonatomic, readonly) NSInteger horizontalItemSpacing;
@property (nonatomic, readonly) NSInteger verticalItemSpacing;
@property (nonatomic, readonly) UIEdgeInsets minEdgeInsets;
@property (nonatomic, readonly) BOOL centeredGrid;

Expand All @@ -115,7 +117,7 @@ typedef enum {
@property (nonatomic, readonly) CGSize contentSize;

// Protocol methods implemented in base class
- (void)setupItemSize:(CGSize)itemSize andItemSpacing:(NSInteger)spacing withMinEdgeInsets:(UIEdgeInsets)edgeInsets andCenteredGrid:(BOOL)centered;
- (void)setupItemSize:(CGSize)itemSize horizontalItemSpacing:(NSInteger)hSpacing verticalItemSpacing:(NSInteger)vSpacing withMinEdgeInsets:(UIEdgeInsets)edgeInsets andCenteredGrid:(BOOL)centered;

// Helpers
- (void)setEdgeAndContentSizeFromAbsoluteContentSize:(CGSize)actualContentSize;
Expand Down
76 changes: 39 additions & 37 deletions GMGridView/GMGridViewLayoutStrategies.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,27 @@ @implementation GMGridViewLayoutStrategyFactory

@implementation GMGridViewLayoutStrategyBase

@synthesize type = _type;
@synthesize type = _type;

@synthesize itemSize = _itemSize;
@synthesize itemSpacing = _itemSpacing;
@synthesize minEdgeInsets = _minEdgeInsets;
@synthesize centeredGrid = _centeredGrid;
@synthesize itemSize = _itemSize;
@synthesize horizontalItemSpacing = _horizontalItemSpacing;
@synthesize verticalItemSpacing = _verticalItemSpacing;
@synthesize minEdgeInsets = _minEdgeInsets;
@synthesize centeredGrid = _centeredGrid;

@synthesize itemCount = _itemCount;
@synthesize edgeInsets = _edgeInsets;
@synthesize gridBounds = _gridBounds;
@synthesize contentSize = _contentSize;
@synthesize itemCount = _itemCount;
@synthesize edgeInsets = _edgeInsets;
@synthesize gridBounds = _gridBounds;
@synthesize contentSize = _contentSize;


- (void)setupItemSize:(CGSize)itemSize andItemSpacing:(NSInteger)spacing withMinEdgeInsets:(UIEdgeInsets)edgeInsets andCenteredGrid:(BOOL)centered
- (void)setupItemSize:(CGSize)itemSize horizontalItemSpacing:(NSInteger)hSpacing verticalItemSpacing:(NSInteger)vSpacing withMinEdgeInsets:(UIEdgeInsets)edgeInsets andCenteredGrid:(BOOL)centered;
{
_itemSize = itemSize;
_itemSpacing = spacing;
_minEdgeInsets = edgeInsets;
_centeredGrid = centered;
_itemSize = itemSize;
_horizontalItemSpacing = hSpacing;
_verticalItemSpacing = vSpacing;
_minEdgeInsets = edgeInsets;
_centeredGrid = centered;
}

- (void)setEdgeAndContentSizeFromAbsoluteContentSize:(CGSize)actualContentSize
Expand Down Expand Up @@ -155,15 +157,15 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds

_numberOfItemsPerRow = 1;

while ((self.numberOfItemsPerRow + 1) * (self.itemSize.width + self.itemSpacing) - self.itemSpacing <= actualBounds.size.width)
while ((self.numberOfItemsPerRow + 1) * (self.itemSize.width + self.horizontalItemSpacing) - self.horizontalItemSpacing <= actualBounds.size.width)
{
_numberOfItemsPerRow++;
}

NSInteger numberOfRows = ceil(self.itemCount / (1.0 * self.numberOfItemsPerRow));

CGSize actualContentSize = CGSizeMake(ceil(MIN(self.itemCount, self.numberOfItemsPerRow) * (self.itemSize.width + self.itemSpacing)) - self.itemSpacing,
ceil(numberOfRows * (self.itemSize.height + self.itemSpacing)) - self.itemSpacing);
CGSize actualContentSize = CGSizeMake(ceil(MIN(self.itemCount, self.numberOfItemsPerRow) * (self.itemSize.width + self.horizontalItemSpacing)) - self.horizontalItemSpacing,
ceil(numberOfRows * (self.itemSize.height + self.verticalItemSpacing)) - self.verticalItemSpacing);

[self setEdgeAndContentSizeFromAbsoluteContentSize:actualContentSize];
}
Expand All @@ -177,8 +179,8 @@ - (CGPoint)originForItemAtPosition:(NSInteger)position
NSUInteger col = position % self.numberOfItemsPerRow;
NSUInteger row = position / self.numberOfItemsPerRow;

origin = CGPointMake(col * (self.itemSize.width + self.itemSpacing) + self.edgeInsets.left,
row * (self.itemSize.height + self.itemSpacing) + self.edgeInsets.top);
origin = CGPointMake(col * (self.itemSize.width + self.horizontalItemSpacing) + self.edgeInsets.left,
row * (self.itemSize.height + self.verticalItemSpacing) + self.edgeInsets.top);
}

return origin;
Expand All @@ -189,8 +191,8 @@ - (NSInteger)itemPositionFromLocation:(CGPoint)location
CGPoint relativeLocation = CGPointMake(location.x - self.edgeInsets.left,
location.y - self.edgeInsets.top);

int col = (int) (relativeLocation.x / (self.itemSize.width + self.itemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.itemSpacing));
int col = (int) (relativeLocation.x / (self.itemSize.width + self.horizontalItemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.verticalItemSpacing));

int position = col + row * self.numberOfItemsPerRow;

Expand Down Expand Up @@ -220,7 +222,7 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
CGPoint contentOffset = CGPointMake(MAX(0, offset.x),
MAX(0, offset.y));

CGFloat itemHeight = self.itemSize.height + self.itemSpacing;
CGFloat itemHeight = self.itemSize.height + self.verticalItemSpacing;

CGFloat firstRow = MAX(0, (int)(contentOffset.y / itemHeight) - 1);

Expand Down Expand Up @@ -271,15 +273,15 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds

_numberOfItemsPerColumn = 1;

while ((_numberOfItemsPerColumn + 1) * (self.itemSize.height + self.itemSpacing) - self.itemSpacing <= actualBounds.size.height)
while ((_numberOfItemsPerColumn + 1) * (self.itemSize.height + self.verticalItemSpacing) - self.verticalItemSpacing <= actualBounds.size.height)
{
_numberOfItemsPerColumn++;
}

NSInteger numberOfColumns = ceil(self.itemCount / (1.0 * self.numberOfItemsPerColumn));

CGSize actualContentSize = CGSizeMake(ceil(numberOfColumns * (self.itemSize.width + self.itemSpacing)) - self.itemSpacing,
ceil(MIN(self.itemCount, self.numberOfItemsPerColumn) * (self.itemSize.height + self.itemSpacing)) - self.itemSpacing);
CGSize actualContentSize = CGSizeMake(ceil(numberOfColumns * (self.itemSize.width + self.horizontalItemSpacing)) - self.horizontalItemSpacing,
ceil(MIN(self.itemCount, self.numberOfItemsPerColumn) * (self.itemSize.height + self.verticalItemSpacing)) - self.verticalItemSpacing);

[self setEdgeAndContentSizeFromAbsoluteContentSize:actualContentSize];
}
Expand All @@ -293,8 +295,8 @@ - (CGPoint)originForItemAtPosition:(NSInteger)position
NSUInteger col = position / self.numberOfItemsPerColumn;
NSUInteger row = position % self.numberOfItemsPerColumn;

origin = CGPointMake(col * (self.itemSize.width + self.itemSpacing) + self.edgeInsets.left,
row * (self.itemSize.height + self.itemSpacing) + self.edgeInsets.top);
origin = CGPointMake(col * (self.itemSize.width + self.horizontalItemSpacing) + self.edgeInsets.left,
row * (self.itemSize.height + self.verticalItemSpacing) + self.edgeInsets.top);
}

return origin;
Expand All @@ -305,8 +307,8 @@ - (NSInteger)itemPositionFromLocation:(CGPoint)location
CGPoint relativeLocation = CGPointMake(location.x - self.edgeInsets.left,
location.y - self.edgeInsets.top);

int col = (int) (relativeLocation.x / (self.itemSize.width + self.itemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.itemSpacing));
int col = (int) (relativeLocation.x / (self.itemSize.width + self.horizontalItemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.verticalItemSpacing));

int position = row + col * self.numberOfItemsPerColumn;

Expand Down Expand Up @@ -336,7 +338,7 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
CGPoint contentOffset = CGPointMake(MAX(0, offset.x),
MAX(0, offset.y));

CGFloat itemWidth = self.itemSize.width + self.itemSpacing;
CGFloat itemWidth = self.itemSize.width + self.horizontalItemSpacing;

CGFloat firstCol = MAX(0, (int)(contentOffset.x / itemWidth) - 1);

Expand Down Expand Up @@ -376,16 +378,16 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds

NSInteger gridContentMaxWidth = self.gridBounds.size.width - self.minEdgeInsets.right - self.minEdgeInsets.left;

while ((self.numberOfItemsPerRow + 1) * (self.itemSize.width + self.itemSpacing) - self.itemSpacing <= gridContentMaxWidth)
while ((self.numberOfItemsPerRow + 1) * (self.itemSize.width + self.horizontalItemSpacing) - self.horizontalItemSpacing <= gridContentMaxWidth)
{
_numberOfItemsPerRow++;
}

_numberOfItemsPerPage = _numberOfItemsPerRow * _numberOfItemsPerColumn;
_numberOfPages = ceil(self.itemCount * 1.0 / self.numberOfItemsPerPage);

CGSize onePageSize = CGSizeMake(self.numberOfItemsPerRow * (self.itemSize.width + self.itemSpacing) - self.itemSpacing,
self.numberOfItemsPerColumn * (self.itemSize.height + self.itemSpacing) - self.itemSpacing);
CGSize onePageSize = CGSizeMake(self.numberOfItemsPerRow * (self.itemSize.width + self.horizontalItemSpacing) - self.horizontalItemSpacing,
self.numberOfItemsPerColumn * (self.itemSize.height + self.verticalItemSpacing) - self.verticalItemSpacing);

if (self.centeredGrid)
{
Expand Down Expand Up @@ -421,8 +423,8 @@ - (CGPoint)originForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSIn
CGPoint offset = CGPointMake(page * self.gridBounds.size.width,
0);

CGFloat x = column * (self.itemSize.width + self.itemSpacing) + self.edgeInsets.left;
CGFloat y = row * (self.itemSize.height + self.itemSpacing) + self.edgeInsets.top;
CGFloat x = column * (self.itemSize.width + self.horizontalItemSpacing) + self.edgeInsets.left;
CGFloat y = row * (self.itemSize.height + self.verticalItemSpacing) + self.edgeInsets.top;

return CGPointMake(x + offset.x,
y + offset.y);
Expand Down Expand Up @@ -472,8 +474,8 @@ - (NSInteger)itemPositionFromLocation:(CGPoint)location
CGPoint relativeLocation = CGPointMake(location.x - originForFirstItemInPage.x,
location.y - originForFirstItemInPage.y);

int col = (int) (relativeLocation.x / (self.itemSize.width + self.itemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.itemSpacing));
int col = (int) (relativeLocation.x / (self.itemSize.width + self.horizontalItemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.verticalItemSpacing));

int position = [self positionForItemAtColumn:col row:row page:page];

Expand Down