diff --git a/GMGridView/GMGridView.h b/GMGridView/GMGridView.h index 439d2da..999bd09 100644 --- a/GMGridView/GMGridView.h +++ b/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 diff --git a/GMGridView/GMGridView.m b/GMGridView/GMGridView.m index 1b9726c..9096df8 100644 --- a/GMGridView/GMGridView.m +++ b/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)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]; diff --git a/GMGridView/GMGridViewLayoutStrategies.h b/GMGridView/GMGridViewLayoutStrategies.h index 5b816b0..2a806b6 100644 --- a/GMGridView/GMGridViewLayoutStrategies.h +++ b/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; diff --git a/GMGridView/GMGridViewLayoutStrategies.m b/GMGridView/GMGridViewLayoutStrategies.m index 71919ad..1bfe125 100644 --- a/GMGridView/GMGridViewLayoutStrategies.m +++ b/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];