Skip to content
This repository
Browse code

Added LTR and TTB horizontal paged strategy

  • Loading branch information...
commit 2e226b599219bc07aee26a728f79f36d4eef3c36 1 parent 9ee51dd
Gulam Moledina authored December 11, 2011
30  GMGridView/API/GMGridViewLayoutStrategies.h
@@ -36,7 +36,8 @@
36 36
 typedef enum {
37 37
     GMGridViewLayoutVertical = 0,
38 38
     GMGridViewLayoutHorizontal,
39  
-    GMGridViewLayoutHorizontalPaged
  39
+    GMGridViewLayoutHorizontalPagedLTR,
  40
+    GMGridViewLayoutHorizontalPagedTTB
40 41
 } GMGridViewLayoutStrategyType;
41 42
 
42 43
 
@@ -98,7 +99,6 @@ typedef enum {
98 99
     NSInteger _itemCount;
99 100
     UIEdgeInsets _edgeInsets;
100 101
     CGRect _gridBounds;
101  
-    CGRect _contentBounds;
102 102
     CGSize _contentSize;
103 103
 }
104 104
 
@@ -112,7 +112,6 @@ typedef enum {
112 112
 @property (nonatomic, readonly) NSInteger itemCount;
113 113
 @property (nonatomic, readonly) UIEdgeInsets edgeInsets;
114 114
 @property (nonatomic, readonly) CGRect gridBounds;
115  
-@property (nonatomic, readonly) CGRect contentBounds;
116 115
 @property (nonatomic, readonly) CGSize contentSize;
117 116
 
118 117
 // Protocol methods implemented in base class
@@ -153,10 +152,10 @@ typedef enum {
153 152
 
154 153
 
155 154
 //////////////////////////////////////////////////////////////
156  
-#pragma mark - Horizontal strategy
  155
+#pragma mark - Horizontal Paged strategy (LTR behavior)
157 156
 //////////////////////////////////////////////////////////////
158 157
 
159  
-@interface GMGridViewLayoutHorizontalPagedStrategy : GMGridViewLayoutHorizontalStrategy <GMGridViewLayoutStrategy>
  158
+@interface GMGridViewLayoutHorizontalPagedStrategy : GMGridViewLayoutHorizontalStrategy
160 159
 {
161 160
     @protected
162 161
     NSInteger _numberOfItemsPerRow;
@@ -168,6 +167,27 @@ typedef enum {
168 167
 @property (nonatomic, readonly) NSInteger numberOfItemsPerPage;
169 168
 @property (nonatomic, readonly) NSInteger numberOfPages;
170 169
 
  170
+
  171
+// These 3 methods can be implemented by child classes
  172
+- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page;
  173
+- (NSInteger)columnForItemAtPosition:(NSInteger)position;
  174
+- (NSInteger)rowForItemAtPosition:(NSInteger)position;
  175
+
171 176
 @end
172 177
 
173 178
 
  179
+//////////////////////////////////////////////////////////////
  180
+#pragma mark - Horizontal Paged Left to Right strategy
  181
+//////////////////////////////////////////////////////////////
  182
+
  183
+@interface GMGridViewLayoutHorizontalPagedLTRStrategy : GMGridViewLayoutHorizontalPagedStrategy
  184
+
  185
+@end
  186
+
  187
+//////////////////////////////////////////////////////////////
  188
+#pragma mark - Horizontal Paged Top To Bottom strategy
  189
+//////////////////////////////////////////////////////////////
  190
+
  191
+@interface GMGridViewLayoutHorizontalPagedTTBStrategy : GMGridViewLayoutHorizontalPagedStrategy
  192
+
  193
+@end
133  GMGridView/API/GMGridViewLayoutStrategies.m
@@ -43,13 +43,15 @@ @implementation GMGridViewLayoutStrategyFactory
43 43
         case GMGridViewLayoutVertical:
44 44
             strategy = [[GMGridViewLayoutVerticalStrategy alloc] init];
45 45
             break;
46  
-        case GMGridViewLayoutHorizontalPaged:
47  
-            strategy = [[GMGridViewLayoutHorizontalPagedStrategy alloc] init];
48  
-            break;
49 46
         case GMGridViewLayoutHorizontal:
50  
-        default:
51 47
             strategy = [[GMGridViewLayoutHorizontalStrategy alloc] init];
52 48
             break;
  49
+        case GMGridViewLayoutHorizontalPagedLTR:
  50
+            strategy = [[GMGridViewLayoutHorizontalPagedLTRStrategy alloc] init];
  51
+            break;
  52
+        case GMGridViewLayoutHorizontalPagedTTB:
  53
+            strategy = [[GMGridViewLayoutHorizontalPagedTTBStrategy alloc] init];
  54
+            break;
53 55
     }
54 56
     
55 57
     return strategy;
@@ -76,7 +78,6 @@ @implementation GMGridViewLayoutStrategyBase
76 78
 @synthesize itemCount     = _itemCount;
77 79
 @synthesize edgeInsets    = _edgeInsets;
78 80
 @synthesize gridBounds    = _gridBounds;
79  
-@synthesize contentBounds = _contentBounds;
80 81
 @synthesize contentSize   = _contentSize;
81 82
 
82 83
 
@@ -165,12 +166,6 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
165 166
                                ceil(numberOfRows * (self.itemSize.height + self.itemSpacing)) - self.itemSpacing);
166 167
     
167 168
     [self setEdgeAndContentSizeFromAbsoluteContentSize:actualContentSize];
168  
-    
169  
-    _contentBounds = CGRectMake(actualBounds.origin.x + _edgeInsets.left, 
170  
-                                actualBounds.origin.y + _edgeInsets.top, 
171  
-                                actualBounds.size.width -_edgeInsets.left - _edgeInsets.right, 
172  
-                                actualBounds.size.height - _edgeInsets.top - _edgeInsets.bottom);
173  
-    
174 169
 }
175 170
 
176 171
 - (CGPoint)originForItemAtPosition:(NSInteger)position
@@ -229,7 +224,7 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
229 224
     
230 225
     CGFloat firstRow = MAX(0, (int)(contentOffset.y / itemHeight) - 1);
231 226
 
232  
-    CGFloat lastRow = ceil((contentOffset.y + self.contentBounds.size.height) / itemHeight);
  227
+    CGFloat lastRow = ceil((contentOffset.y + self.gridBounds.size.height) / itemHeight);
233 228
     
234 229
     NSInteger firstPosition = firstRow * self.numberOfItemsPerRow;
235 230
     NSInteger lastPosition  = ((lastRow + 1) * self.numberOfItemsPerRow);
@@ -287,11 +282,6 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
287 282
                                ceil(MIN(self.itemCount, self.numberOfItemsPerColumn) * (self.itemSize.height + self.itemSpacing)) - self.itemSpacing);
288 283
     
289 284
     [self setEdgeAndContentSizeFromAbsoluteContentSize:actualContentSize];
290  
-    
291  
-    _contentBounds = CGRectMake(actualBounds.origin.x + _edgeInsets.left, 
292  
-                                actualBounds.origin.y + _edgeInsets.top, 
293  
-                                actualBounds.size.width -_edgeInsets.left - _edgeInsets.right, 
294  
-                                actualBounds.size.height - _edgeInsets.top - _edgeInsets.bottom);
295 285
 }
296 286
 
297 287
 - (CGPoint)originForItemAtPosition:(NSInteger)position
@@ -350,7 +340,7 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
350 340
     
351 341
     CGFloat firstCol = MAX(0, (int)(contentOffset.x / itemWidth) - 1);
352 342
     
353  
-    CGFloat lastCol = ceil((contentOffset.x + self.contentBounds.size.width) / itemWidth);
  343
+    CGFloat lastCol = ceil((contentOffset.x + self.gridBounds.size.width) / itemWidth);
354 344
     
355 345
     NSInteger firstPosition = firstCol * self.numberOfItemsPerColumn;
356 346
     NSInteger lastPosition  = ((lastCol + 1) * self.numberOfItemsPerColumn);
@@ -378,25 +368,10 @@ + (BOOL)requiresEnablingPaging
378 368
     return YES;
379 369
 }
380 370
 
381  
-- (id)init
382  
-{
383  
-    if ((self = [super init])) 
384  
-    {
385  
-        _type = GMGridViewLayoutHorizontalPaged;
386  
-    }
387  
-    
388  
-    return self;
389  
-}
390  
-
391 371
 - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
392 372
 {
393 373
     [super rebaseWithItemCount:count insideOfBounds:bounds];
394 374
     
395  
-    CGRect actualBounds = CGRectMake(0, 
396  
-                                     0, 
397  
-                                     bounds.size.width  - self.minEdgeInsets.right - self.minEdgeInsets.left, 
398  
-                                     bounds.size.height - self.minEdgeInsets.top   - self.minEdgeInsets.bottom);
399  
-    
400 375
     _numberOfItemsPerRow = 1;
401 376
     
402 377
     NSInteger gridContentMaxWidth = self.gridBounds.size.width - self.minEdgeInsets.right - self.minEdgeInsets.left;
@@ -434,13 +409,6 @@ - (void)rebaseWithItemCount:(NSInteger)count insideOfBounds:(CGRect)bounds
434 409
     
435 410
     _contentSize = CGSizeMake((onePageSize.width  + self.edgeInsets.left + self.edgeInsets.right) * self.numberOfPages, 
436 411
                               onePageSize.height + self.edgeInsets.top  + self.edgeInsets.bottom);
437  
-    
438  
-    _contentBounds = CGRectMake(actualBounds.origin.x + _edgeInsets.left, 
439  
-                                actualBounds.origin.y + _edgeInsets.top, 
440  
-                               actualBounds.size.width -_edgeInsets.left - _edgeInsets.right, 
441  
-                                actualBounds.size.height - _edgeInsets.top - _edgeInsets.bottom);
442  
-    
443  
-    
444 412
 }
445 413
 
446 414
 - (NSInteger)pageForItemAtIndex:(NSInteger)index
@@ -460,19 +428,35 @@ - (CGPoint)originForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSIn
460 428
                        y + offset.y);
461 429
 }
462 430
 
  431
+- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page
  432
+{
  433
+    return column + row * self.numberOfItemsPerRow + (page * self.numberOfItemsPerPage); 
  434
+}
  435
+
  436
+- (NSInteger)columnForItemAtPosition:(NSInteger)position
  437
+{
  438
+    position %= self.numberOfItemsPerPage;
  439
+    return position % self.numberOfItemsPerRow;;
  440
+}
  441
+
  442
+- (NSInteger)rowForItemAtPosition:(NSInteger)position
  443
+{
  444
+    position %= self.numberOfItemsPerPage;
  445
+    return floor(position / self.numberOfItemsPerRow);
  446
+}
  447
+
463 448
 - (CGPoint)originForItemAtPosition:(NSInteger)position
464 449
 {
465 450
     NSUInteger page = [self pageForItemAtIndex:position];
466 451
     
467 452
     position %= self.numberOfItemsPerPage;
468  
-    
469  
-    NSUInteger column = position % self.numberOfItemsPerRow;
470  
-    NSUInteger row = floor(position / self.numberOfItemsPerRow);
  453
+        
  454
+    NSUInteger row = [self rowForItemAtPosition:position];
  455
+    NSUInteger column = [self columnForItemAtPosition:position];
471 456
     
472 457
     CGPoint origin = [self originForItemAtColumn:column row:row page:page];
473 458
     
474 459
     return origin;
475  
-     
476 460
 }
477 461
 
478 462
 - (NSInteger)itemPositionFromLocation:(CGPoint)location
@@ -491,7 +475,7 @@ - (NSInteger)itemPositionFromLocation:(CGPoint)location
491 475
     int col = (int) (relativeLocation.x / (self.itemSize.width + self.itemSpacing)); 
492 476
     int row = (int) (relativeLocation.y / (self.itemSize.height + self.itemSpacing));
493 477
     
494  
-    int position = col + row * self.numberOfItemsPerRow + (page * self.numberOfItemsPerPage);
  478
+    int position = [self positionForItemAtColumn:col row:row page:page];
495 479
  
496 480
     if (position >= [self itemCount] || position < 0) 
497 481
     {
@@ -529,3 +513,62 @@ - (NSRange)rangeOfPositionsInBoundsFromOffset:(CGPoint)offset
529 513
 
530 514
 @end
531 515
 
  516
+
  517
+//////////////////////////////////////////////////////////////
  518
+#pragma mark - 
  519
+#pragma mark - HorizontalPagedLTR strategy implementation
  520
+//////////////////////////////////////////////////////////////
  521
+
  522
+@implementation GMGridViewLayoutHorizontalPagedLTRStrategy
  523
+
  524
+- (id)init
  525
+{
  526
+    if ((self = [super init])) 
  527
+    {
  528
+        _type = GMGridViewLayoutHorizontalPagedLTR;
  529
+    }
  530
+    
  531
+    return self;
  532
+}
  533
+
  534
+// Nothing to change, LTR is already the behavior of the base class
  535
+
  536
+@end
  537
+
  538
+
  539
+//////////////////////////////////////////////////////////////
  540
+#pragma mark - 
  541
+#pragma mark - HorizontalPagedRTL strategy implementation
  542
+//////////////////////////////////////////////////////////////
  543
+
  544
+@implementation GMGridViewLayoutHorizontalPagedTTBStrategy
  545
+
  546
+- (id)init
  547
+{
  548
+    if ((self = [super init])) 
  549
+    {
  550
+        _type = GMGridViewLayoutHorizontalPagedTTB;
  551
+    }
  552
+    
  553
+    return self;
  554
+}
  555
+
  556
+- (NSInteger)positionForItemAtColumn:(NSInteger)column row:(NSInteger)row page:(NSInteger)page
  557
+{
  558
+    return row + column * self.numberOfItemsPerColumn + (page * self.numberOfItemsPerPage); 
  559
+}
  560
+
  561
+- (NSInteger)columnForItemAtPosition:(NSInteger)position
  562
+{
  563
+    position %= self.numberOfItemsPerPage;
  564
+    return floor(position / self.numberOfItemsPerColumn);
  565
+}
  566
+
  567
+- (NSInteger)rowForItemAtPosition:(NSInteger)position
  568
+{
  569
+    position %= self.numberOfItemsPerPage;
  570
+    return position % self.numberOfItemsPerColumn;
  571
+}
  572
+
  573
+@end
  574
+
17  GMGridView/OptionsViewController.m
@@ -242,7 +242,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
242 242
                 
243 243
                 switch ([self.gridView.layoutStrategy type]) 
244 244
                 {
245  
-                    case GMGridViewLayoutHorizontalPaged:
  245
+                    case GMGridViewLayoutHorizontalPagedTTB:
  246
+                        [pickerView selectRow:3 inComponent:0 animated:YES];
  247
+                        break;
  248
+                    case GMGridViewLayoutHorizontalPagedLTR:
246 249
                         [pickerView selectRow:2 inComponent:0 animated:YES];
247 250
                         break;
248 251
                     case GMGridViewLayoutHorizontal:
@@ -378,7 +381,10 @@ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComp
378 381
             self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontal];
379 382
             break;
380 383
         case 2:
381  
-            self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPaged];
  384
+            self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedLTR];
  385
+            break;
  386
+        case 3:
  387
+            self.gridView.layoutStrategy = [GMGridViewLayoutStrategyFactory strategyFromType:GMGridViewLayoutHorizontalPagedTTB];
382 388
             break;
383 389
         case 0:
384 390
         default:
@@ -394,7 +400,7 @@ - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
394 400
 
395 401
 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
396 402
 {
397  
-    return 3;
  403
+    return 4;
398 404
 }
399 405
 
400 406
 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
@@ -409,7 +415,10 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f
409 415
             title = @"Horizontal strategy";
410 416
             break;
411 417
         case 2:
412  
-            title = @"Horizontal paged strategy";
  418
+            title = @"Horizontal paged LTR strategy";
  419
+            break;
  420
+        case 3:
  421
+            title = @"Horizontal paged TTB strategy";
413 422
             break;
414 423
         default:
415 424
             title = @"Unknown";

0 notes on commit 2e226b5

Please sign in to comment.
Something went wrong with that request. Please try again.