【发布时间】: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