Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Horizontal and vertical item spacing #52

Open
wants to merge 1 commit into from

5 participants

@brotherbard

I separated the item spacing into horizontal and vertical to allow for better layout customization.

@steipete

Looks good! Only thing I'm asking myself is if we should still use spacing as a parameter and CGSize for width/height instead of two parameters.

@brotherbard

I created separate properties because I don't think of the spacing as a size and didn't want to overload CGSize to mean something else. But if people prefer it as CGSize I'll change it.

Also, as I've been playing with the spacing I think I want to change it based on portrait or landscape. The margins as well. I'm currently abusing GMGridView:sizeForItemsInInterfaceOrientation: to change these settings but maybe there should be a separate delegate call for that. And I'm not sure if it should be the data source delegate so a new layout delegate? Any preferences? Or is that the correct place to change things.

@gmoledina
Owner

I really don't think we should handle this level of detail for the spacing as it will get hard to manage. Later we might even need different spacing per orientation too, and it just gets too much.

I suggest the grid only handles the minimum and the when creating the cell, you can play with your own subviews and layers and add more spacing if required.

@dibsme

Thanks. It works charm.

@morteza2128

Thanks alOOOt
Horizontal and vertical item spacing were necessary !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  GMGridView/GMGridView.h
@@ -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
View
18 GMGridView/GMGridView.m
@@ -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;
@@ -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;
@@ -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];
}
@@ -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];
View
10 GMGridView/GMGridViewLayoutStrategies.h
@@ -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;
@@ -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;
@@ -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;
@@ -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;
View
76 GMGridView/GMGridViewLayoutStrategies.m
@@ -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
@@ -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];
}
@@ -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;
@@ -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;
@@ -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);
@@ -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];
}
@@ -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;
@@ -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;
@@ -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);
@@ -376,7 +378,7 @@ - (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++;
}
@@ -384,8 +386,8 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
_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)
{
@@ -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);
@@ -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];
Something went wrong with that request. Please try again.