Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added extra tap delegate + fixed contentoffset on rotation with pagin…

…g enabled
  • Loading branch information...
commit e575ce29e4f815a2a0e159b31d49880d039b6ff1 1 parent dcf1243
Gulam Moledina authored
2  GMGridView/GMGridView.h
View
@@ -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;
87 GMGridView/GMGridView.m
View
@@ -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;
@@ -121,7 +122,7 @@ - (void)queueReusableCell:(GMGridViewCell *)cell;
- (void)receivedMemoryWarningNotification:(NSNotification *)notification;
// Rotation handling
-- (void)willRotate:(NSNotification *)notification;
+- (void)receivedWillRotateNotification:(NSNotification *)notification;
@end
@@ -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
@@ -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
{
@@ -359,12 +367,11 @@ - (void)receivedMemoryWarningNotification:(NSNotification *)notification
[_reusableCells removeAllObjects];
}
-- (void)willRotate:(NSNotification *)notification
+- (void)receivedWillRotateNotification:(NSNotification *)notification
{
_rotationActive = YES;
}
-
//////////////////////////////////////////////////////////////
#pragma mark Setters / getters
//////////////////////////////////////////////////////////////
@@ -1101,7 +1108,7 @@ - (void)transformingGestureDidFinish
}
//////////////////////////////////////////////////////////////
-#pragma mark Tap
+#pragma mark Tap gesture
//////////////////////////////////////////////////////////////
- (void)tapGestureUpdated:(UITapGestureRecognizer *)tapGesture
@@ -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];
+ }
}
//////////////////////////////////////////////////////////////
@@ -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
@@ -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;
@@ -1674,7 +1703,7 @@ - (void)swapObjectAtIndex:(NSInteger)index1 withObjectAtIndex:(NSInteger)index2
//////////////////////////////////////////////////////////////
-#pragma mark public methods
+#pragma mark depracated public methods
//////////////////////////////////////////////////////////////
- (UIScrollView *)scrollView
16 GMGridView/GMGridView.xcodeproj/project.pbxproj
View
@@ -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 = (
@@ -91,6 +102,7 @@
78509317149FAC2E000787E4 /* GMGridView */ = {
isa = PBXGroup;
children = (
+ 16B1745C150D3460005928D5 /* Additions */,
78509335149FAC61000787E4 /* GMGridView-Constants.h */,
78509336149FAC61000787E4 /* GMGridView.h */,
78509337149FAC61000787E4 /* GMGridView.m */,
@@ -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>";
Please sign in to comment.
Something went wrong with that request. Please try again.