檢查this video正確理解我的問題。UICollectionView垂直流不返回中心的第一個單元格
我有一個自定義的視圖組成,其中包含一個UICollectionView,用戶可以使用兩個按鈕向上/向下滾動。這些值應該在變化時居中,一切都很好,直到我返回到比中心低得多的第一個值。請注意,對於水平滾動視圖使用相同方法我沒有任何問題。
我對從X軸居中複製的值進行居中的方法只是改變了Y軸的相關部分。
創建一個UICollectionViewFlowLayout派生類並將其設置爲UICollectionView的UICollectioViewFlowLayout。請注意,以編程方式滾動時,不會調用override方法。
[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屬性。