Xamarin iOS UICollectionView在刷卡后居中

时间:2016-12-16 08:45:07

标签: ios xamarin uicollectionview

我需要当用户在水平布局CollectionView上滑动时,完成滑动后,所选单元格将设置在视图中心。

1 个答案:

答案 0 :(得分:0)

需要做的事情:

    ctor ()
    {
        CollectionView.Delegate = null; //Optionally
        CollectionView.WillEndDragging += CollectionViewOnWillEndDragging;
    }

    private void CitiesCollectionViewOnWillEndDragging(object sender, WillEndDraggingEventArgs eventArgs)
    {
        eventArgs.TargetContentOffset = AdjustTargetContentOffset(eventArgs.TargetContentOffset);
    }

    private CGPoint AdjustTargetContentOffset(CGPoint eventArgs)
    {
        var countScrolledCells = (nint)(eventArgs.X / _cellWidth);
        var offset = eventArgs.X % _cellWidth;

        if (offset > _cellWidth / 2)
        {
            countScrolledCells++;
        }


        return new CGPoint(_cellWidth * countScrolledCells, 0);

    }

http://blog.karmadust.com/centered-paging-with-preview-cells-on-uicollectionview/