Permalink
Browse files

Added LTR and TTB horizontal paged strategy

  • Loading branch information...
gmoledina committed Dec 12, 2011
1 parent 9ee51dd commit 2e226b599219bc07aee26a728f79f36d4eef3c36
@@ -36,7 +36,8 @@
typedef enum {
GMGridViewLayoutVertical = 0,
GMGridViewLayoutHorizontal,
- GMGridViewLayoutHorizontalPaged
+ GMGridViewLayoutHorizontalPagedLTR,
+ GMGridViewLayoutHorizontalPagedTTB
} GMGridViewLayoutStrategyType;
@@ -98,7 +99,6 @@ typedef enum {
NSInteger _itemCount;
UIEdgeInsets _edgeInsets;
CGRect _gridBounds;
- CGRect _contentBounds;
CGSize _contentSize;
}
@@ -112,7 +112,6 @@ typedef enum {
@property (nonatomic, readonly) NSInteger itemCount;
@property (nonatomic, readonly) UIEdgeInsets edgeInsets;
@property (nonatomic, readonly) CGRect gridBounds;
-@property (nonatomic, readonly) CGRect contentBounds;
@property (nonatomic, readonly) CGSize contentSize;
// Protocol methods implemented in base class
@@ -153,10 +152,10 @@ typedef enum {
//////////////////////////////////////////////////////////////
-#pragma mark - Horizontal strategy
+#pragma mark - Horizontal Paged strategy (LTR behavior)
//////////////////////////////////////////////////////////////
-@interface GMGridViewLayoutHorizontalPagedStrategy : GMGridViewLayoutHorizontalStrategy <GMGridViewLayoutStrategy>
+@interface GMGridViewLayoutHorizontalPagedStrategy : GMGridViewLayoutHorizontalStrategy
{
@protected
NSInteger _numberOfItemsPerRow;
@@ -168,6 +167,27 @@ typedef enum {
@property (nonatomic, readonly) NSInteger numberOfItemsPerPage;
@property (nonatomic, readonly) NSInteger numberOfPages;
+
+// These 3 methods can be implemented by child classes
+- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page;
+- (NSInteger)columnForItemAtPosition:(NSInteger)position;
+- (NSInteger)rowForItemAtPosition:(NSInteger)position;
+
@end
+//////////////////////////////////////////////////////////////
+#pragma mark - Horizontal Paged Left to Right strategy
+//////////////////////////////////////////////////////////////
+
+@interface GMGridViewLayoutHorizontalPagedLTRStrategy : GMGridViewLayoutHorizontalPagedStrategy
+
+@end
+
+//////////////////////////////////////////////////////////////
+#pragma mark - Horizontal Paged Top To Bottom strategy
+//////////////////////////////////////////////////////////////
+
+@interface GMGridViewLayoutHorizontalPagedTTBStrategy : GMGridViewLayoutHorizontalPagedStrategy
+
+@end
@@ -43,13 +43,15 @@ @implementation GMGridViewLayoutStrategyFactory
case GMGridViewLayoutVertical:
strategy = [[GMGridViewLayoutVerticalStrategy alloc] init];
break;
- case GMGridViewLayoutHorizontalPaged:
- strategy = [[GMGridViewLayoutHorizontalPagedStrategy alloc] init];
- break;
case GMGridViewLayoutHorizontal:
- default:
strategy = [[GMGridViewLayoutHorizontalStrategy alloc] init];
break;
+ case GMGridViewLayoutHorizontalPagedLTR:
+ strategy = [[GMGridViewLayoutHorizontalPagedLTRStrategy alloc] init];
+ break;
+ case GMGridViewLayoutHorizontalPagedTTB:
+ strategy = [[GMGridViewLayoutHorizontalPagedTTBStrategy alloc] init];
+ break;
}
return strategy;
@@ -76,7 +78,6 @@ @implementation GMGridViewLayoutStrategyBase
@synthesize itemCount = _itemCount;
@synthesize edgeInsets = _edgeInsets;
@synthesize gridBounds = _gridBounds;
-@synthesize contentBounds = _contentBounds;
@synthesize contentSize = _contentSize;
@@ -165,12 +166,6 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
ceil(numberOfRows * (self.itemSize.height + self.itemSpacing)) - self.itemSpacing);
[self setEdgeAndContentSizeFromAbsoluteContentSize:actualContentSize];
-
- _contentBounds = CGRectMake(actualBounds.origin.x + _edgeInsets.left,
- actualBounds.origin.y + _edgeInsets.top,
- actualBounds.size.width -_edgeInsets.left - _edgeInsets.right,
- actualBounds.size.height - _edgeInsets.top - _edgeInsets.bottom);
-
}
- (CGPoint)originForItemAtPosition:(NSInteger)position
@@ -229,7 +224,7 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
CGFloat firstRow = MAX(0, (int)(contentOffset.y / itemHeight) - 1);
- CGFloat lastRow = ceil((contentOffset.y + self.contentBounds.size.height) / itemHeight);
+ CGFloat lastRow = ceil((contentOffset.y + self.gridBounds.size.height) / itemHeight);
NSInteger firstPosition = firstRow * self.numberOfItemsPerRow;
NSInteger lastPosition = ((lastRow + 1) * self.numberOfItemsPerRow);
@@ -287,11 +282,6 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
ceil(MIN(self.itemCount, self.numberOfItemsPerColumn) * (self.itemSize.height + self.itemSpacing)) - self.itemSpacing);
[self setEdgeAndContentSizeFromAbsoluteContentSize:actualContentSize];
-
- _contentBounds = CGRectMake(actualBounds.origin.x + _edgeInsets.left,
- actualBounds.origin.y + _edgeInsets.top,
- actualBounds.size.width -_edgeInsets.left - _edgeInsets.right,
- actualBounds.size.height - _edgeInsets.top - _edgeInsets.bottom);
}
- (CGPoint)originForItemAtPosition:(NSInteger)position
@@ -350,7 +340,7 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
CGFloat firstCol = MAX(0, (int)(contentOffset.x / itemWidth) - 1);
- CGFloat lastCol = ceil((contentOffset.x + self.contentBounds.size.width) / itemWidth);
+ CGFloat lastCol = ceil((contentOffset.x + self.gridBounds.size.width) / itemWidth);
NSInteger firstPosition = firstCol * self.numberOfItemsPerColumn;
NSInteger lastPosition = ((lastCol + 1) * self.numberOfItemsPerColumn);
@@ -378,25 +368,10 @@ + (BOOL)requiresEnablingPaging
return YES;
}
-- (id)init
-{
- if ((self = [super init]))
- {
- _type = GMGridViewLayoutHorizontalPaged;
- }
-
- return self;
-}
-
- (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
{
[super rebaseWithItemCount:count insideOfBounds:bounds];
- CGRect actualBounds = CGRectMake(0,
- 0,
- bounds.size.width - self.minEdgeInsets.right - self.minEdgeInsets.left,
- bounds.size.height - self.minEdgeInsets.top - self.minEdgeInsets.bottom);
-
_numberOfItemsPerRow = 1;
NSInteger gridContentMaxWidth = self.gridBounds.size.width - self.minEdgeInsets.right - self.minEdgeInsets.left;
@@ -434,13 +409,6 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
_contentSize = CGSizeMake((onePageSize.width + self.edgeInsets.left + self.edgeInsets.right) * self.numberOfPages,
onePageSize.height + self.edgeInsets.top + self.edgeInsets.bottom);
-
- _contentBounds = CGRectMake(actualBounds.origin.x + _edgeInsets.left,
- actualBounds.origin.y + _edgeInsets.top,
- actualBounds.size.width -_edgeInsets.left - _edgeInsets.right,
- actualBounds.size.height - _edgeInsets.top - _edgeInsets.bottom);
-
-
}
- (NSInteger)pageForItemAtIndex:(NSInteger)index
@@ -460,19 +428,35 @@ - (CGPoint)originForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSIn
y + offset.y);
}
+- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page
+{
+ return column + row * self.numberOfItemsPerRow + (page * self.numberOfItemsPerPage);
+}
+
+- (NSInteger)columnForItemAtPosition:(NSInteger)position
+{
+ position %= self.numberOfItemsPerPage;
+ return position % self.numberOfItemsPerRow;;
+}
+
+- (NSInteger)rowForItemAtPosition:(NSInteger)position
+{
+ position %= self.numberOfItemsPerPage;
+ return floor(position / self.numberOfItemsPerRow);
+}
+
- (CGPoint)originForItemAtPosition:(NSInteger)position
{
NSUInteger page = [self pageForItemAtIndex:position];
position %= self.numberOfItemsPerPage;
-
- NSUInteger column = position % self.numberOfItemsPerRow;
- NSUInteger row = floor(position / self.numberOfItemsPerRow);
+
+ NSUInteger row = [self rowForItemAtPosition:position];
+ NSUInteger column = [self columnForItemAtPosition:position];
CGPoint origin = [self originForItemAtColumn:column row:row page:page];
return origin;
-
}
- (NSInteger)itemPositionFromLocation:(CGPoint)location
@@ -491,7 +475,7 @@ - (NSInteger)itemPositionFromLocation:(CGPoint)location
int col = (int) (relativeLocation.x / (self.itemSize.width + self.itemSpacing));
int row = (int) (relativeLocation.y / (self.itemSize.height + self.itemSpacing));
- int position = col + row * self.numberOfItemsPerRow + (page * self.numberOfItemsPerPage);
+ int position = [self positionForItemAtColumn:col row:row page:page];
if (position >= [self itemCount] || position < 0)
{
@@ -529,3 +513,62 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
@end
+
+//////////////////////////////////////////////////////////////
+#pragma mark -
+#pragma mark - HorizontalPagedLTR strategy implementation
+//////////////////////////////////////////////////////////////
+
+@implementation GMGridViewLayoutHorizontalPagedLTRStrategy
+
+- (id)init
+{
+ if ((self = [super init]))
+ {
+ _type = GMGridViewLayoutHorizontalPagedLTR;
+ }
+
+ return self;
+}
+
+// Nothing to change, LTR is already the behavior of the base class
+
+@end
+
+
+//////////////////////////////////////////////////////////////
+#pragma mark -
+#pragma mark - HorizontalPagedRTL strategy implementation
+//////////////////////////////////////////////////////////////
+
+@implementation GMGridViewLayoutHorizontalPagedTTBStrategy
+
+- (id)init
+{
+ if ((self = [super init]))
+ {
+ _type = GMGridViewLayoutHorizontalPagedTTB;
+ }
+
+ return self;
+}
+
+- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page
+{
+ return row + column * self.numberOfItemsPerColumn + (page * self.numberOfItemsPerPage);
+}
+
+- (NSInteger)columnForItemAtPosition:(NSInteger)position
+{
+ position %= self.numberOfItemsPerPage;
+ return floor(position / self.numberOfItemsPerColumn);
+}
+
+- (NSInteger)rowForItemAtPosition:(NSInteger)position
+{
+ position %= self.numberOfItemsPerPage;
+ return position % self.numberOfItemsPerColumn;
+}
+
+@end
+
@@ -242,7 +242,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
switch ([self.gridView.layoutStrategy type])
{
- case GMGridViewLayoutHorizontalPaged:
+ case GMGridViewLayoutHorizontalPagedTTB:
+ [pickerView selectRow:3 inComponent:0 animated:YES];
+ break;
+ case GMGridViewLayoutHorizontalPagedLTR:
[pickerView selectRow:2 inComponent:0 animated:YES];
break;
case GMGridViewLayoutHorizontal:
@@ -378,7 +381,10 @@ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComp
self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontal];
break;
case 2:
- self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPaged];
+ self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedLTR];
+ break;
+ case 3:
+ self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedTTB];
break;
case 0:
default:
@@ -394,7 +400,7 @@ - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
- return 3;
+ return 4;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
@@ -409,7 +415,10 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f
title = @"Horizontal strategy";
break;
case 2:
- title = @"Horizontal paged strategy";
+ title = @"Horizontal paged LTR strategy";
+ break;
+ case 3:
+ title = @"Horizontal paged TTB strategy";
break;
default:
title = @"Unknown";

0 comments on commit 2e226b5

Please sign in to comment.