Permalink
Browse files

Added two new delegate methods to sortingDelegate that ask for

permission to move an item and to insert an item at a position.
  • Loading branch information...
1 parent c65fb44 commit 23c4698332871d8e259330904063543da562e36e @Bringo committed Feb 11, 2012
Showing with 20 additions and 0 deletions.
  1. +4 −0 GMGridView/API/GMGridView.h
  2. +16 −0 GMGridView/API/GMGridView.m
@@ -145,6 +145,10 @@ typedef enum
- (void)GMGridView:(GMGridView *)gridView didEndMovingCell:(GMGridViewCell *)cell;
// Enable/Disable the shaking behavior of an item being moved
- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowShakingBehaviorWhenMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index;
+// Allow/Disallow moving of an item
+- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index;
+// Allow/Disallow placing item at index
+- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowMovingCell:(GMGridViewCell *)view toIndex:(NSInteger)index;
@end
@@ -456,6 +456,14 @@ - (void)sortingLongPressGestureUpdated:(UILongPressGestureRecognizer *)longPress
NSInteger position = [self.layoutStrategy itemPositionFromLocation:location];
+ // Ask the delegate if moving is permitted
+ if ([self.sortingDelegate respondsToSelector:@selector(GMGridView:shouldAllowMovingCell:atIndex:)])
+ {
+ GMGridViewCell *item = [self cellForItemAtIndex:position];
+ if (![self.sortingDelegate GMGridView:self shouldAllowMovingCell:item atIndex:position])
+ position = GMGV_INVALID_POSITION;
+ }
+
if (position != GMGV_INVALID_POSITION)
{
[self sortingMoveDidStartAtPoint:location];
@@ -671,6 +679,14 @@ - (void)sortingMoveDidContinueToPoint:(CGPoint)point
int position = [self.layoutStrategy itemPositionFromLocation:point];
int tag = position + kTagOffset;
+ // Ask the delegate if inserting item is permitted
+ if ([self.sortingDelegate respondsToSelector:@selector(GMGridView:shouldAllowMovingCell:toIndex:)])
+ {
+ GMGridViewCell *item = [self cellForItemAtIndex:position];
+ if (![self.sortingDelegate GMGridView:self shouldAllowMovingCell:item toIndex:position])
+ position = GMGV_INVALID_POSITION;
+ }
+
if (position != GMGV_INVALID_POSITION && position != _sortFuturePosition && position < _numberTotalItems)
{
BOOL positionTaken = NO;

1 comment on commit 23c4698

@Bringo
Owner
Bringo commented on 23c4698 Feb 11, 2012

Extended the sortingDelegate with two more optional delegate methods:

- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowMovingCell:(GMGridViewCell *)view atIndex:(NSInteger)index; is called when a move is about to start. If this method returns NO, the move won't take place.

- (BOOL)GMGridView:(GMGridView *)gridView shouldAllowMovingCell:(GMGridViewCell *)view toIndex:(NSInteger)index; is called when an item is moved to a new position in the grid. If this method returns NO, the insertion won't take place.

Please sign in to comment.