IUICollectionViewDelegateFlowLayout拖放功能:长按单元格下方的单元格正在移动

时间:2017-02-17 04:36:14

标签: ios xamarin.ios uicollectionviewlayout

我已将以下手势识别器附加到我的收藏视图中:

public void AttachLongPressGestureRecognizer()
        {
            // Create a custom gesture recognizer
            var longPressGesture = new UILongPressGestureRecognizer((gesture) =>
            {
                // Take action based on state
                switch (gesture.State)
                {
                    case UIGestureRecognizerState.Began:
                        var selectedIndexPath = this.MyCollectionView.IndexPathForItemAtPoint(gesture.LocationInView(View));

                        if (selectedIndexPath != null)
                        {
                            this.MyCollectionView.BeginInteractiveMovementForItem(selectedIndexPath);
                        }
                        break;

                    case UIGestureRecognizerState.Changed:
                        this.MyCollectionView.UpdateInteractiveMovement(gesture.LocationInView(View));
                        break;

                    case UIGestureRecognizerState.Ended:
                        this.MyCollectionView.EndInteractiveMovement();
                        break;

                    default:
                        this.MyCollectionView.CancelInteractiveMovement();
                        break;
                }

            });

虽然拖放功能似乎正常,但收到的selectedIndexPath不正确。当我长按特定的单元格时,它下面的单元格被拾取并移动。我根据屏幕尺寸使用以下内容调整单元格大小:

[Export("collectionView:layout:sizeForItemAtIndexPath:")]
    public CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
    {
        var cellHeight = collectionView.Frame.Height * 0.3;
        var cellWidth = collectionView.Frame.Width * 0.45;
        return new CoreGraphics.CGSize(cellWidth, cellHeight);
    }

单击同一单元格会返回正确的indexPath,但长按会返回被按下单元格下方单元格的indexPath。

1 个答案:

答案 0 :(得分:0)

之所以发生这种情况,是因为其他视图的高度干扰了收集高度。