UI-重新排序 UI 具有全角和动态高度单元格的 CollectionView 时出现问题
UI-Issues while reordering UICollectionView with full-width and dynamic height cells
我有一个 CollectionView,其中的单元格具有全宽和动态(自动布局计算)高度。
我通过设置估计身高来做到这一点:
flowLayout.estimatedItemSize = CGSize(width: 200, height: 10)
然后在委托方法中返回全宽和任意高度。
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 10)
}
然后在单元格中执行以下操作:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = self.systemLayoutSizeFitting(layoutAttributes.size)
var newFrame = layoutAttributes.frame
// note: don't change the width
newFrame.size.height = ceil(size.height)
layoutAttributes.frame = newFrame
return layoutAttributes
}
一切都按预期工作...单元格大小符合预期!太棒了!
现在我想添加重新排序...
我添加了一个 GestureRecognizer 并让它重新排列我的单元格..
@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
guard let view = gestureRecognizer.view else { return }
let location = gestureRecognizer.location(in: view)
switch gestureRecognizer.state {
case .began:
guard let selectedIndexPath = collectionView.indexPathForItem(at: location) else { break }
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
collectionView.updateInteractiveMovementTargetPosition(location)
case .ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}
这基本上也可以工作...但是在重新排序期间,我的单元格会奇怪地动画...这些单元格有时会将它们的大小减小到我的 10 大小,但不会让单元格自行测量。放开拖拽后,它又可以工作了...
我注意到当我缓存我的 CollectionViewCells 的高度时,它会好一点,但对于我以前从未见过的所有单元格,它仍然是一个 10 点高的单元格。
为了在重新排序期间使单元格大小正确,我缺少什么?
我也有一个有这个问题的示例项目,可以在这里找到:https://www.dropbox.com/s/ya6zald4wa6kg10/CollectionViewTester.zip?dl=0
问题是,重新排序自动调整大小的单元格时,单元格大小会更改为 estimatedItemSize
。
有雷达报告 here。
我有一个 CollectionView,其中的单元格具有全宽和动态(自动布局计算)高度。
我通过设置估计身高来做到这一点:
flowLayout.estimatedItemSize = CGSize(width: 200, height: 10)
然后在委托方法中返回全宽和任意高度。
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 10)
}
然后在单元格中执行以下操作:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = self.systemLayoutSizeFitting(layoutAttributes.size)
var newFrame = layoutAttributes.frame
// note: don't change the width
newFrame.size.height = ceil(size.height)
layoutAttributes.frame = newFrame
return layoutAttributes
}
一切都按预期工作...单元格大小符合预期!太棒了!
现在我想添加重新排序... 我添加了一个 GestureRecognizer 并让它重新排列我的单元格..
@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {
guard let view = gestureRecognizer.view else { return }
let location = gestureRecognizer.location(in: view)
switch gestureRecognizer.state {
case .began:
guard let selectedIndexPath = collectionView.indexPathForItem(at: location) else { break }
collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
collectionView.updateInteractiveMovementTargetPosition(location)
case .ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
}
这基本上也可以工作...但是在重新排序期间,我的单元格会奇怪地动画...这些单元格有时会将它们的大小减小到我的 10 大小,但不会让单元格自行测量。放开拖拽后,它又可以工作了...
我注意到当我缓存我的 CollectionViewCells 的高度时,它会好一点,但对于我以前从未见过的所有单元格,它仍然是一个 10 点高的单元格。
为了在重新排序期间使单元格大小正确,我缺少什么?
我也有一个有这个问题的示例项目,可以在这里找到:https://www.dropbox.com/s/ya6zald4wa6kg10/CollectionViewTester.zip?dl=0
问题是,重新排序自动调整大小的单元格时,单元格大小会更改为 estimatedItemSize
。
有雷达报告 here。