当我将一个单元格拖到CollectionView Bounds之外时,我得到了一些奇怪的行为(实际上是崩溃)。如何限制CollectionView的用户交互仅限于CollectionView的框架?当用户长按并拖动视图单元格时,我只希望它们能够在CollectionView中移动单元格,而不是整个屏幕上移动单元格
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();
}
答案 0 :(得分:1)
您应该将手势的位置与ContentView
而不是Frame
进行比较,如:
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;
调整if语句中的常量以满足您的要求。
此外,当您致电MoveItem()
时,事件MyCollectionView.EndInteractiveMovement();
可能会发生崩溃。您可以尝试发布一些有关它的代码。
答案 1 :(得分:0)
您可以使用
collectionView.bounces = false
如果你想拖到边缘。
你还可以发布你遇到的崩溃吗?