Skip to content

Commit

Permalink
Added extra tap delegate + fixed contentoffset on rotation with pagin…
Browse files Browse the repository at this point in the history
…g enabled
  • Loading branch information
gmoledina committed Mar 11, 2012
1 parent dcf1243 commit e575ce2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 33 deletions.
2 changes: 2 additions & 0 deletions GMGridView/GMGridView.h
Expand Up @@ -137,6 +137,8 @@ typedef enum
- (void)GMGridView:(GMGridView *)gridView didTapOnItemAtIndex:(NSInteger)position;

@optional
// Tap on space without any items
- (void)GMGridViewDidTapOnEmptySpace:(GMGridView *)gridView;
// Called when the delete-button has been pressed. Required to enable editing mode.
// This method wont delete the cell automatically. Call the delete method of the gridView when appropriate.
- (void)GMGridView:(GMGridView *)gridView processDeleteActionForItemAtIndex:(NSInteger)index;
Expand Down
87 changes: 58 additions & 29 deletions GMGridView/GMGridView.m
Expand Up @@ -111,6 +111,7 @@ - (GMGridViewCell *)cellForItemAtIndex:(NSInteger)position;
- (GMGridViewCell *)newItemSubViewForPosition:(NSInteger)position;
- (NSInteger)positionForItemSubview:(GMGridViewCell *)view;
- (void)setSubviewsCacheAsInvalid;
- (CGRect)rectForPoint:(CGPoint)point inPaggingMode:(BOOL)pagging;

// Lazy loading
- (void)loadRequiredItems;
Expand All @@ -121,7 +122,7 @@ - (void)queueReusableCell:(GMGridViewCell *)cell;
- (void)receivedMemoryWarningNotification:(NSNotification *)notification;

// Rotation handling
- (void)willRotate:(NSNotification *)notification;
- (void)receivedWillRotateNotification:(NSNotification *)notification;

@end

Expand Down Expand Up @@ -258,7 +259,7 @@ - (void)commonInit
_reusableCells = [[NSMutableSet alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedMemoryWarningNotification:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRotate:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedWillRotateNotification:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
}

- (void)dealloc
Expand Down Expand Up @@ -342,6 +343,13 @@ - (void)layoutSubviews
[self applyWithoutAnimation:^{
[self layoutSubviewsWithAnimation:GMGridViewItemAnimationNone];
}];

// Fixing the contentOffset when pagging enabled

if (self.pagingEnabled)
{
[self setContentOffset:[self rectForPoint:self.contentOffset inPaggingMode:YES].origin animated:YES];
}
}
else
{
Expand All @@ -359,12 +367,11 @@ - (void)receivedMemoryWarningNotification:(NSNotification *)notification
[_reusableCells removeAllObjects];
}

- (void)willRotate:(NSNotification *)notification
- (void)receivedWillRotateNotification:(NSNotification *)notification
{
_rotationActive = YES;
}


//////////////////////////////////////////////////////////////
#pragma mark Setters / getters
//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1101,7 +1108,7 @@ - (void)transformingGestureDidFinish
}

//////////////////////////////////////////////////////////////
#pragma mark Tap
#pragma mark Tap gesture
//////////////////////////////////////////////////////////////

- (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture
Expand All @@ -1113,6 +1120,10 @@ - (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture
{
[self.actionDelegate GMGridView:self didTapOnItemAtIndex:position];
}
else if([self.actionDelegate respondsToSelector:@selector(GMGridViewDidTapOnEmptySpace:)])
{
[self.actionDelegate GMGridViewDidTapOnEmptySpace:self];
}
}

//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1285,6 +1296,43 @@ - (void)relayoutItemsAnimated:(BOOL)animated
}
}

- (CGRect)rectForPoint:(CGPoint)point inPaggingMode:(BOOL)pagging
{
CGRect targetRect = CGRectZero;

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

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

CGFloat pageX = ceilf(point.x / pageSize.width);
CGFloat pageY = ceilf(point.y / pageSize.height);

originScroll = CGPointMake(pageX * pageSize.width,
pageY *pageSize.height);

/*
while (originScroll.x + pageSize.width < point.x)
{
originScroll.x += pageSize.width;
}
while (originScroll.y + pageSize.height < point.y)
{
originScroll.y += pageSize.height;
}
*/
targetRect = CGRectMake(originScroll.x, originScroll.y, pageSize.width, pageSize.height);
}
else
{
targetRect = CGRectMake(point.x, point.y, _itemSize.width, _itemSize.height);
}

return targetRect;
}

//////////////////////////////////////////////////////////////
#pragma mark loading/destroying items & reusing cells
Expand Down Expand Up @@ -1482,38 +1530,19 @@ - (void)scrollToObjectAtIndex:(NSInteger)index atScrollPosition:(GMGridViewScrol
index = MIN(index, _numberTotalItems);

CGPoint origin = [self.layoutStrategy originForItemAtPosition:index];
CGRect targetRect;
CGRect targetRect = [self rectForPoint:origin inPaggingMode:self.pagingEnabled];

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

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

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

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

targetRect = CGRectMake(originScroll.x, originScroll.y, pageSize.width, pageSize.height);
}
else
if (!self.pagingEnabled)
{
CGRect gridRect = CGRectMake(origin.x, origin.y, _itemSize.width, _itemSize.height);
targetRect = self.bounds;

switch (scrollPosition)
{
case GMGridViewScrollPositionNone:
default:
targetRect = gridRect; // no special coordinate handling
break;

case GMGridViewScrollPositionTop:
targetRect.origin.y = gridRect.origin.y; // set target y origin to cell's y origin
break;
Expand Down Expand Up @@ -1674,7 +1703,7 @@ - (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2


//////////////////////////////////////////////////////////////
#pragma mark public methods
#pragma mark depracated public methods
//////////////////////////////////////////////////////////////

- (UIScrollView *)scrollView
Expand Down
16 changes: 12 additions & 4 deletions GMGridView/GMGridView.xcodeproj/project.pbxproj
Expand Up @@ -60,6 +60,17 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
16B1745C150D3460005928D5 /* Additions */ = {
isa = PBXGroup;
children = (
16A0360E14A012E50062437D /* UIGestureRecognizer+GMGridViewAdditions.h */,
16A0360F14A012E50062437D /* UIGestureRecognizer+GMGridViewAdditions.m */,
16A0361314A012EF0062437D /* UIView+GMGridViewAdditions.h */,
16A0361414A012EF0062437D /* UIView+GMGridViewAdditions.m */,
);
name = Additions;
sourceTree = "<group>";
};
78509307149FAC2E000787E4 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -91,6 +102,7 @@
78509317149FAC2E000787E4 /* GMGridView */ = {
isa = PBXGroup;
children = (
16B1745C150D3460005928D5 /* Additions */,
78509335149FAC61000787E4 /* GMGridView-Constants.h */,
78509336149FAC61000787E4 /* GMGridView.h */,
78509337149FAC61000787E4 /* GMGridView.m */,
Expand All @@ -99,10 +111,6 @@
7850933A149FAC61000787E4 /* GMGridViewCell+Extended.h */,
7850933B149FAC61000787E4 /* GMGridViewLayoutStrategies.h */,
7850933C149FAC61000787E4 /* GMGridViewLayoutStrategies.m */,
16A0360E14A012E50062437D /* UIGestureRecognizer+GMGridViewAdditions.h */,
16A0360F14A012E50062437D /* UIGestureRecognizer+GMGridViewAdditions.m */,
16A0361314A012EF0062437D /* UIView+GMGridViewAdditions.h */,
16A0361414A012EF0062437D /* UIView+GMGridViewAdditions.m */,
);
path = GMGridView;
sourceTree = "<group>";
Expand Down

0 comments on commit e575ce2

Please sign in to comment.