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();
}