【问题标题】:UICollectionView vertical flow doesn't center first cell when going back返回时 UICollectionView 垂直流不居中第一个单元格
【发布时间】:2017-01-03 21:41:47
【问题描述】:

检查this video 以准确了解我的问题。

我有一个自定义视图组合,其中包含一个 UICollectionView,用户可以使用两个按钮向上/向下滚动。值应该在更改时居中,一切正常,直到我返回到第一个值,该值看起来比中心低得多。请注意,我对水平滚动视图使用相同的方法没有任何问题。

我的值居中方法是从 X 轴居中复制的,只是更改了 Y 轴的相关部分。

创建了一个 UICollectionViewFlowLayout 派生类并设置为 UICollectionView 的 UICollectioViewFlowLayout。请注意,以编程方式滚动时不会调用覆盖方法。

[Register("CenterVerticalCellCollectionViewFlowLayout")]
public class CenterVerticalCellCollectionViewFlowLayout : UICollectionViewFlowLayout
{
    public CenterVerticalCellCollectionViewFlowLayout(IntPtr ptr) : base(ptr)
    {

    }

    public override CGPoint TargetContentOffset(CGPoint proposedContentOffset, 
                                                CGPoint scrollingVelocity)
    {
        var cv = this.CollectionView;
        var cvBounds = cv.Bounds;
        var halfHeight = cvBounds.Height * 0.5;
        var proposedContentOffsetCenterY = proposedContentOffset.Y + halfHeight;
        var attributesForVisibleCells = LayoutAttributesForElementsInRect(cvBounds);
        UICollectionViewLayoutAttributes candidateAttributes = null;

        foreach (var attributes in attributesForVisibleCells)
        {
            // == Skip comparison with non-cell items (headers and footers) == //
            if (attributes.RepresentedElementCategory != UICollectionElementCategory.Cell)
            {
                continue;
            }

            if ((attributes.Center.Y == 0) || 
                (attributes.Center.Y > (cv.ContentOffset.Y + halfHeight) && 
                 scrollingVelocity.Y < 0))
            {
                continue;
            }

            // == First time in the loop == //
            if (candidateAttributes == null)
            {
                candidateAttributes = new UICollectionViewLayoutAttributes();
            }
            else
            {
                candidateAttributes = attributes;
                continue;
            }

            var a = attributes.Center.Y- proposedContentOffsetCenterY;
            var b = candidateAttributes.Center.Y - proposedContentOffsetCenterY;


            if (Math.Abs(a) < Math.Abs(b))
            {
                candidateAttributes = attributes;
            }
        }

        // Beautification step , I don't know why it works!
        if (proposedContentOffset.Y == -(cv.ContentInset.Top))
        {
            return proposedContentOffset;
        }

        if (candidateAttributes == null)
        {
            candidateAttributes = new UICollectionViewLayoutAttributes();
        }

        return new CGPoint(Math.Floor(candidateAttributes.Center.Y - 
            halfHeight), proposedContentOffset.Y);
    }
}

加载自定义 UIView 时,我设置了以下内容。

CollectionView.ContentOffset = CGPoint.Empty;

UIViewController.ViewWillLayoutSubviews我调用下面的方法。

CollectionView.CollectionViewLayout.InvalidateLayout();

UIViewController.ViewDidLayoutSubviews 我打电话给下面。

var insets = CollectionView.ContentInset;
var value = (View.Frame.Height - (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize.Height) * 0.5;

insets.Top = (nfloat)value;
insets.Bottom = (nfloat)value;
CollectionView.ContentInset = insets;
CollectionView.DecelerationRate = UIScrollView.DecelerationRateFast;

最后是下面的代码以编程方式向上/向下滚动。

partial void UpButtonTouchUpInside(UIButton sender)
{
    MoveSelectionCollectionViewCell();
}

partial void DownButtonTouchUpInside(UIButton sender)
{
    MoveSelectionCollectionViewCell(false);
}

void MoveSelectionCollectionViewCell(bool moveUp = true)
{
    var firstVisibleIndexPath = CollectionView.IndexPathsForVisibleItems.FirstOrDefault();

    if (firstVisibleIndexPath != null)
    {
        var row = moveUp ? firstVisibleIndexPath.Row + 1 : firstVisibleIndexPath.Row - 1;
        if (row > -1 &&
            row < _source.Collection.Count)
        {
            var scrollAtIndexPath = NSIndexPath.FromRowSection(row, 0);
            CollectionView.ScrollToItem(
                scrollAtIndexPath,
                UICollectionViewScrollPosition.CenteredVertically,
                true);
        }
    }
}

请参阅下面的 UICollectionView IB 属性。

【问题讨论】:

    标签: ios xamarin xamarin.ios uicollectionview


    【解决方案1】:

    回顾我的问题让我意识到问题所在!

    当在UIViewController.ViewDidLayoutSubviews 调用代码时,它会考虑View.Frame.Height 这是错误的,因为自定义控件视图的大小与UICollectionView 的大小不同,因此证明了这个问题。

    改变这个:

    var value = (View.Frame.Height - (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize.Height) * 0.5;
    

    到这里:

    var value = (CollectionView.Frame.Height - (CollectionView.CollectionViewLayout as UICollectionViewFlowLayout).ItemSize.Height) * 0.5;
    

    成功了。

    【讨论】:

      猜你喜欢
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多