gpt4 book ai didi

ios - 如何实现 iOS 9 的 UICollectionView 交互移动功能?

转载 作者:行者123 更新时间:2023-11-28 06:31:37 25 4
gpt4 key购买 nike

iOS 9 添加了点击并按住以轻松重新排列 Collection View 单元格的功能。在微不足道的情况下启用此功能非常简单。只需在您的 UICollectionViewController 上实现一个功能。

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)

如果您需要有关该函数的更多文档,请参阅 UICollectionViewDataSource 中的定义。

虽然这只适用于自定义布局。如何让它在更复杂的情况下发挥作用?

最佳答案

它没有很好的文档记录,但有 2 个强制覆盖可以使其正常工作。在您的 UICollectionViewLayout 子类中实现以下 2 个函数

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

首先,您需要确保布局中的项目属性被过滤为包含给定矩形中的属性。如果您包含更多内容,它会变得困惑。

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

不确定为什么这是必要的,您可能认为 UIKit 可以根据之前的调用推断出正确的属性,但事实并非如此。无论如何,您只需要能够为给定的 indexPath 获取正确的属性。

关于ios - 如何实现 iOS 9 的 UICollectionView 交互移动功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40168345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com