Skip to content

Commit

Permalink
Added horizontal paged strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
gmoledina committed Dec 11, 2011
1 parent 4161cdd commit 9ee51dd
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 55 deletions.
2 changes: 1 addition & 1 deletion GMGridView/API/GMGridView.h
Expand Up @@ -82,7 +82,7 @@ typedef enum
- (void)removeObjectAtIndex:(NSInteger)index;
- (void)reloadObjectAtIndex:(NSInteger)index;
- (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2;
- (void)scrollToObjectAtIndex:(NSInteger)index;
- (void)scrollToObjectAtIndex:(NSInteger)index animated:(BOOL)animated;

@end

Expand Down
62 changes: 36 additions & 26 deletions GMGridView/API/GMGridView.m
Expand Up @@ -273,6 +273,8 @@ - (void)setMainSuperView:(UIView *)mainSuperView
- (void)setLayoutStrategy:(id<GMGridViewLayoutStrategy>)layoutStrategy
{
_layoutStrategy = layoutStrategy;

_scrollView.pagingEnabled = [[self.layoutStrategy class] requiresEnablingPaging];
[self setNeedsLayout];
}

Expand Down Expand Up @@ -1073,8 +1075,7 @@ - (void)recomputeSize

CGSize contentSize = [self.layoutStrategy contentSize];

_minPossibleContentOffset = CGPointMake(-1 * (_scrollView.contentInset.left),
-1 * (_scrollView.contentInset.top));
_minPossibleContentOffset = CGPointMake(0, 0);
_maxPossibleContentOffset = CGPointMake(contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right,
contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom);

Expand Down Expand Up @@ -1270,12 +1271,11 @@ - (void)reloadObjectAtIndex:(NSInteger)index

currentView.tag = kTagOffset - 1;

// Better performance animating ourselves instead of using animated:YES in scrollRectToVisible
[UIView animateWithDuration:kDefaultAnimationDuration
delay:0
options:kDefaultAnimationOptions
animations:^{
[_scrollView scrollRectToVisible:cell.frame animated:NO];
[self scrollToObjectAtIndex:index animated:NO];
currentView.alpha = 0;
cell.alpha = 1;
}
Expand All @@ -1288,18 +1288,40 @@ - (void)reloadObjectAtIndex:(NSInteger)index
[self setSubviewsCacheAsInvalid];
}

- (void)scrollToObjectAtIndex:(NSInteger)index
- (void)scrollToObjectAtIndex:(NSInteger)index animated:(BOOL)animated
{
NSAssert((index >= 0 && index < _numberTotalItems), @"Invalid index");

index = MAX(0, index);
index = MIN(index, _numberTotalItems);

CGPoint origin = [self.layoutStrategy originForItemAtPosition:index];
CGRect scrollToRect = CGRectMake(origin.x, origin.y, _itemSize.width, _itemSize.height);

if (_scrollView.pagingEnabled)
{
CGPoint originScroll = CGPointZero;

CGSize pageSize = CGSizeMake(_scrollView.bounds.size.width - _scrollView.contentInset.left - _scrollView.contentInset.right,
_scrollView.bounds.size.height - _scrollView.contentInset.top - _scrollView.contentInset.bottom);

while (originScroll.x + pageSize.width < origin.x)
{
originScroll.x += pageSize.width;
}

while (originScroll.y + pageSize.height < origin.y)
{
originScroll.y += pageSize.height;
}

scrollToRect = CGRectMake(originScroll.x, originScroll.y, pageSize.width, pageSize.height);
}

// Better performance animating ourselves instead of using animated:YES in scrollRectToVisible
[UIView animateWithDuration:kDefaultAnimationDuration
[UIView animateWithDuration:animated ? kDefaultAnimationDuration : 0
delay:0
options:kDefaultAnimationOptions
animations:^{
[_scrollView scrollRectToVisible:CGRectMake(origin.x, origin.y, _itemSize.width, _itemSize.height) animated:NO];
[_scrollView scrollRectToVisible:scrollToRect animated:NO];
}
completion:^(BOOL finished){
}
Expand All @@ -1311,12 +1333,9 @@ - (void)insertObjectAtIndex:(NSInteger)index
NSAssert((index >= 0 && index <= _numberTotalItems), @"Invalid index specified");

GMGridViewCell *cell = nil;
BOOL isInsertedObjectVisible = NO;

if (index >= self.firstPositionLoaded && index <= self.lastPositionLoaded)
{
isInsertedObjectVisible = YES;

{
cell = [self newItemSubViewForPosition:index];

for (int i = index; i < _numberTotalItems; i++)
Expand All @@ -1331,23 +1350,17 @@ - (void)insertObjectAtIndex:(NSInteger)index
_numberTotalItems++;
[self recomputeSize];

// We cant use cell.frame as it might not be loaded yet
CGPoint newObjectOrigin = [self.layoutStrategy originForItemAtPosition:index];

// Better performance animating ourselves instead of using animated:YES in scrollRectToVisible
[UIView animateWithDuration:kDefaultAnimationDuration
delay:0
options:kDefaultAnimationOptions
animations:^{
[_scrollView scrollRectToVisible:CGRectMake(newObjectOrigin.x, newObjectOrigin.y, _itemSize.width, _itemSize.height) animated:NO];
[self scrollToObjectAtIndex:index animated:NO];
}
completion:^(BOOL finished){
[self setNeedsLayout];
}
];



[self setSubviewsCacheAsInvalid];
}

Expand All @@ -1366,17 +1379,14 @@ - (void)removeObjectAtIndex:(NSInteger)index
cell.tag = kTagOffset - 1;
_numberTotalItems--;

CGPoint origin = [self.layoutStrategy originForItemAtPosition:index];

// Better performance animating ourselves instead of using animated:YES in scrollRectToVisible
[UIView animateWithDuration:kDefaultAnimationDuration
delay:0
options:kDefaultAnimationOptions
animations:^{
cell.contentView.alpha = 0.3;
cell.alpha = 0;

[_scrollView scrollRectToVisible:CGRectMake(origin.x, origin.y, _itemSize.width, _itemSize.height) animated:NO];
[self scrollToObjectAtIndex:index animated:NO];

[self recomputeSize];
}
Expand Down Expand Up @@ -1424,11 +1434,11 @@ - (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2
animations:^{
if (!CGRectIntersectsRect(view2.frame, visibleRect))
{
[_scrollView scrollRectToVisible:view1.frame animated:NO];
[self scrollToObjectAtIndex:index1 animated:NO];
}
else if (!CGRectIntersectsRect(view1.frame, visibleRect))
{
[_scrollView scrollRectToVisible:view2.frame animated:NO];
[self scrollToObjectAtIndex:index2 animated:NO];
}
}
completion:^(BOOL finished){
Expand Down
23 changes: 22 additions & 1 deletion GMGridView/API/GMGridViewLayoutStrategies.h
Expand Up @@ -35,7 +35,8 @@

typedef enum {
GMGridViewLayoutVertical = 0,
GMGridViewLayoutHorizontal
GMGridViewLayoutHorizontal,
GMGridViewLayoutHorizontalPaged
} GMGridViewLayoutStrategyType;


Expand All @@ -57,6 +58,8 @@ typedef enum {

@protocol GMGridViewLayoutStrategy <NSObject>

+ (BOOL)requiresEnablingPaging;

- (GMGridViewLayoutStrategyType)type;

// Setup
Expand Down Expand Up @@ -149,4 +152,22 @@ typedef enum {
@end


//////////////////////////////////////////////////////////////
#pragma mark - Horizontal strategy
//////////////////////////////////////////////////////////////

@interface GMGridViewLayoutHorizontalPagedStrategy : GMGridViewLayoutHorizontalStrategy <GMGridViewLayoutStrategy>
{
@protected
NSInteger _numberOfItemsPerRow;
NSInteger _numberOfItemsPerPage;
NSInteger _numberOfPages;
}

@property (nonatomic, readonly) NSInteger numberOfItemsPerRow;
@property (nonatomic, readonly) NSInteger numberOfItemsPerPage;
@property (nonatomic, readonly) NSInteger numberOfPages;

@end


0 comments on commit 9ee51dd

Please sign in to comment.