UICollectionView动画单元超出范围

Tac*_*Lee 7 animation ios uicollectionview uicollectionviewcell uicollectionviewlayout

我设置了UICollectionView具有以下设置的:

  • collectionView适合屏幕范围
  • 仅应用垂直滚动
  • 大多数单元格适合内容的宽度
  • 一些单元可以在用户交互中动态更改其高度(动画)

它几乎类似于UITableView,在大多数情况下都可以正常工作,除了在特定情况下不应用动画时例外。

例如,在堆叠的单元格中collectionView,上部单元格之一会扩展其高度。然后下部单元必须向下移动以保持距离。如果此移动单元的目标框架超出collectionView's范围,则不会应用任何动画,并且该单元会消失。

相反的情况以相同的方式进行;如果下部单元格的源帧超出屏幕边界(当前在边界之外),并且上部单元格应缩小,则不会应用任何动画,而只会出现在目标帧上。

这似乎在由所控制的内存管理逻辑中是适当的UICollectionView,但与此同时,没有自然的向用户显示某些内容只是突然出现或消失的事实。我已经用UITableView对此进行了测试,并且发生了同样的事情。

有没有解决此问题的方法?

Gaé*_*anZ 5

您应该添加一些代码或至少是您的UI问题的gif。

我试图使用基本的UICollectionViewLayout子类来复制您的问题:

protocol CollectionViewLayoutDelegate: AnyObject {
    func heightForItem(at indexPath: IndexPath) -> CGFloat
}

class CollectionViewLayout: UICollectionViewLayout {
    weak var delegate: CollectionViewLayoutDelegate?

    private var itemAttributes: [UICollectionViewLayoutAttributes] = []

    override func prepare() {
        super.prepare()
        itemAttributes = generateItemAttributes()
    }

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
        return collectionView?.contentOffset ?? .zero
        return super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
    }

    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return itemAttributes.first { $0.indexPath == indexPath }
    }

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return itemAttributes.filter { $0.frame.intersects(rect) }
    }

    private func generateItemAttributes() -> [UICollectionViewLayoutAttributes] {
        var offset: CGFloat = 0
        return (0..<numberOfItems()).map { index in
            let indexPath = IndexPath(item: index, section: 0)
            let frame = CGRect(
                x: 0,
                y: offset,
                width: collectionView?.bounds.width ?? 0,
                height: delegate?.heightForItem(at: indexPath) ?? 0
            )
            offset = frame.maxY
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.frame = frame
            return attributes
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

简而言之UIViewController,每次选择一个单元格时,我都会重新加载第一个单元格:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    updatedIndexPath = IndexPath(item: 0, section: 0)
    collectionView.reloadItems(at: [updatedIndexPath])
}
Run Code Online (Sandbox Code Playgroud)

在那种情况下,我面对这样的动画:

虫子

如何解决?

我认为您可以尝试调整通过super.finalLayoutAttributesForDisappearingItem(at: itemIndexPath)计算其正确框架返回的属性并使用z-index。

但是您也可以像这样简单地尝试使所有布局无效:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    layout = CollectionViewLayout()
    layout.delegate = self
    collectionView.setCollectionViewLayout(layout, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

并覆盖:

override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
    return collectionView?.contentOffset ?? .zero
}
Run Code Online (Sandbox Code Playgroud)

以避免在布局无效时错误的目标内容偏移量计算。

固定