1

I have UICollectionView on which i am displaying the custom cells.I am trying to move the cells by making long press gesture but it does not work at all.

I have followed this tutorial but it does not working for me.Please tell how to do this?

Long Press gesture

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{

    NSLog(@"long press geture called");
    switch(gestureRecognizer.state)
    {
        case UIGestureRecognizerStateBegan:
            p = [gestureRecognizer locationInView:self.collection_view];
          gesture_indexPath = [self.collection_view indexPathForItemAtPoint:p];
        [self.collection_view beginInteractiveMovementForItemAtIndexPath:gesture_indexPath];
            NSLog(@"state began called");

                     break;

        case UIGestureRecognizerStateChanged:

        //    collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
            [self.collection_view updateInteractiveMovementTargetPosition:[gestureRecognizer locationInView:[gestureRecognizer view]]];
            NSLog(@"state changed called");
        case UIGestureRecognizerStateEnded:
            [self.collection_view updateInteractiveMovementTargetPosition:p];
                  NSLog(@"state changed called");
        default:
            [self.collection_view cancelInteractiveMovement];
    }
}

Method Overidden

-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSLog(@"Move at index path called");
}
TechChain
  • 7,104
  • 20
  • 81
  • 193

2 Answers2

12

You're missing break; statements inside the switch-case block. Without a break; at the end of each case section, the switch-case statement isn't exited, and the next line is executed.

For completeness, here's a minimum implementation in Objective-C for a reorderable UICollectionView.

Implementing reordering for a UICollectionView not embedded inside a UICollectionViewController (probably instead inside a UIViewController, but maybe inside a UICollectionViewCell or UITableViewCell if you're getting fancy) requires the following three parts at a minimum:

  1. Inside viewDidLoad (or, if you're embedding a UICollectionView inside a UITableViewCell or a UICollectionViewCell set up in a storyboard, this codes goes inside awakeFromNib), add a UILongPressGestureRecognizer to your UICollectionView like this:

    UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)
    [self.yourCollectionView addGestureRecognizer:longGR];
    
  2. Implement handleLongPress: to react to the UILongPressGestureRecognizer you just added:

    - (void)handleLongPress:(UILongPressGestureRecognizer *)gr{   
        switch(gr.state){
            case UIGestureRecognizerStateBegan:
            {
                NSIndexPath *selectedIndexPath = [self.yourCollectionView indexPathForItemAtPoint:[gr locationInView:self.yourCollectionView]];
                if(selectedIndexPath == nil) break;
                [self.yourCollectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
                break;
            }
            case UIGestureRecognizerStateChanged:
            {
                [self.yourCollectionView updateInteractiveMovementTargetPosition:[gr locationInView:gr.view]];
                break;
            }
            case UIGestureRecognizerStateEnded:
            {
                [self.yourCollectionView endInteractiveMovement];
                break;
            }
            default:
            {
                [self.yourCollectionView cancelInteractiveMovement];
                break;
            }
        }
    }
    
  3. Finally, add the following:

    - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
        //add your data source manipulation logic here
        //specifically, change the order of entries in the data source to match the new visual order of the cells.
        //even without anything inside this function, the cells will move visually if you build and run
    
    }
    

....and the cells will now magically move when you touch, hold, then drag!

(This all assumes that you've implemented the standard UICollectionView dataSource and delegate methods and the UICollectionView is successfully loading/displaying in your app. If you need help with the basic set-up of a UICollectionView, check out this post by Warewolf ).

Community
  • 1
  • 1
adamup
  • 1,480
  • 19
  • 27
0

Your switch statement is falling through (one of the perils of translating a blog post written in Swift to Objective-C). Every call to the handler with a change in touch position executes every statement below the Changed: case, including cancelInteractiveMovement (On another note, that should likely be endInteractiveMovement).

halst
  • 1
  • 1