0

I'm getting some weird behavior (actually a crash) when I drag a cell outside of the CollectionView Bounds. How can I limit the user interaction of the CollectionView to only the frame of the CollectionView? When the user long-presses and drags a view cell, I only want them to be able to move the cell within the CollectionView, and not all over the screen

private void HandleLongPressOnCollection(UILongPressGestureRecognizer gesture)
{
    switch (gesture.State)
    {
        case UIGestureRecognizerState.Began:
            var myIndexPath = MyCollectionView.IndexPathForItemAtPoint(gesture.LocationInView(MyCollectionView));
            this.selectedIndexPath = myIndexPath;

            BeginInteractiveMovementForItem();
            break;
        case UIGestureRecognizerState.Changed: // This is not working correctly
            if (MyCollectionView.Frame.Contains(gesture.LocationInView(MyCollectionView)))
            {
                MyCollectionView.UpdateInteractiveMovement(gesture.LocationInView(MyCollectionView));
            }
            break;
        case UIGestureRecognizerState.Ended:
            EndInteractiveMovementForItem();
            break;
        default:
            MyCollectionView.CancelInteractiveMovement();
            break;
      }
}


private void BeginInteractiveMovementForItem()
{
  if (selectedIndexPath != null)
  {
    MyCollectionView.BeginInteractiveMovementForItem(selectedIndexPath);

    var cell = MyCollectionView.CellForItem(selectedIndexPath) as CustomViewCell;
    cell.MarkCellAsMoving();
  }
}

private void EndInteractiveMovementForItem()
{
  if (selectedIndexPath != null)
  {
    var cell = MyCollectionView.CellForItem(selectedIndexPath) as CustomViewCell;
    cell?.SetToNormalState();
  }
  MyCollectionView.EndInteractiveMovement();
}
stepheaw
  • 1,683
  • 3
  • 22
  • 35

2 Answers2

1

You should compare the gesture's location with ContentView not Framelike:

case UIGestureRecognizerState.Changed:

    CGPoint gesturePoint = gesture.LocationInView(MyCollectionView);

    CGSize contentSize = MyCollectionView.ContentSize;

    if (gesturePoint.X > 0 && gesturePoint.X < contentSize.Width &&
        gesturePoint.Y > 0 && gesturePoint.Y < contentSize.Height)
    {
        MyCollectionView.UpdateInteractiveMovement(gesture.LocationInView(MyCollectionView));
    }
    break;

Adjust the constant in the if statement to feed your requirement.

Moreover the crash may occur in the event MoveItem() when you call MyCollectionView.EndInteractiveMovement();. You can try to post some code about it.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
0

You can use

collectionView.bounces = false

if you want to drag until the edge.

Can you also post the crash you are getting?

Durdu
  • 4,649
  • 2
  • 27
  • 47
  • The crash gives me no stacktrace, and when I look at the device log it does not give me no info either. I think this is a crash related to iOS 11 in xamarin, it does not happen on iOS 10 devices. What I want to do is prevent the user from dragging the cell all over the screen, I only want them to be able to click and drag within the bounds of the collectionview – stepheaw Feb 23 '18 at 16:51