title: UICollectionView effective drag and drop
body: <p>I am currently trying to implement the UITableView reordering behavior using UICollectionView.</p>
  
  <p>Let's call a <strong>UItableView TV</strong> and a <strong>UICollectionView CV</strong> (to clarify the following explanation)</p>
  
  <p>I am basically trying to reproduce the drag&amp;drop of the TV, but I am not using the edit mode, the cell is ready to be moved as soon as the long press gesture is triggered. It works prefectly, I am using the move method of the CV, everything is fine.</p>
  
  <p>I update the contentOffset property of the CV to handle the scroll when the user is dragging a cell. When a user goes to a particular rect at the top and the bottom, I update the contentOffset and the CV scroll. The problem is when the user stop moving it's finger, the gesture doesn't send any update which makes the scroll stop and start again as soon as the user moves his finger.</p>
  
  <p>This behavior is definitely not natural, I would prefer continu to scroll until the user release the CV as it is the case in the TV. The TV drag&amp;drop experience is awesome and I really want to reproduce the same feeling. Does anyone know how they manage the scroll in TV during reordering ?</p>
  
  <ul>
  <li>I tried using a timer to trigger a scroll action repeatedly as long as the gesture position is in the right spot, the scroll was awful and not very productive (very slow and jumpy).</li>
  <li>I also tried using GCD to listen the gesture position in another thread but the result is even worst.</li>
  </ul>
  
  <p>I ran out of idea about that, so if someone has the answer I would marry him!</p>
  
  <p>Here is the implementation of the longPress method:</p>
  
  <pre><code>- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
  {
      ReorganizableCVCLayout *layout = (ReorganizableCVCLayout *)self.collectionView.collectionViewLayout;
      CGPoint gesturePosition = [sender locationInView:self.collectionView];
      NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:gesturePosition];
  
      if (sender.state == UIGestureRecognizerStateBegan)
      {
          layout.selectedItem = selectedIndexPath;
          layout.gesturePoint = gesturePosition; // Setting gesturePoint invalidate layout
      }
      else if (sender.state == UIGestureRecognizerStateChanged)
      {
          layout.gesturePoint = gesturePosition; // Setting gesturePoint invalidate layout
          [self swapCellAtPoint:gesturePosition];
          [self manageScrollWithReferencePoint:gesturePosition];
      }
      else
      {
          [self.collectionView performBatchUpdates:^
          {
              layout.selectedItem = nil;
              layout.gesturePoint = CGPointZero; // Setting gesturePoint invalidate layout
          } completion:^(BOOL completion){[self.collectionView reloadData];}];
      }
  }
  </code></pre>
  
  <p>To make the CV scroll, I am using that method:</p>
  
  <pre><code>- (void)manageScrollWithReferencePoint:(CGPoint)gesturePoint
  {
      ReorganizableCVCLayout *layout = (ReorganizableCVCLayout *)self.collectionView.collectionViewLayout;
      CGFloat topScrollLimit = self.collectionView.contentOffset.y+layout.itemSize.height/2+SCROLL_BORDER;
      CGFloat bottomScrollLimit = self.collectionView.contentOffset.y+self.collectionView.frame.size.height-layout.itemSize.height/2-SCROLL_BORDER;
      CGPoint contentOffset = self.collectionView.contentOffset;
  
      if (gesturePoint.y &lt; topScrollLimit &amp;&amp; gesturePoint.y - layout.itemSize.height/2 - SCROLL_BORDER &gt; 0)
          contentOffset.y -= SCROLL_STEP;
      else if (gesturePoint.y &gt; bottomScrollLimit &amp;&amp;
               gesturePoint.y + layout.itemSize.height/2 + SCROLL_BORDER &lt; self.collectionView.contentSize.height)
          contentOffset.y += SCROLL_STEP;
  
      [self.collectionView setContentOffset:contentOffset];
  }
  </code></pre>