【问题标题】:How do I implement iOS 9's UICollectionView interactive movement features?如何实现 iOS 9 的 UICollectionView 交互移动功能?
【发布时间】:2017-03-03 06:36:07
【问题描述】:

iOS 9 添加了点按并按住以轻松重新排列集合视图单元格的功能。在琐碎的情况下启用此功能很简单。只需在您的 UICollectionViewController 上实现一项功能。

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

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

这并不只是工作自定义布局。如何让它在更复杂的情况下工作?

【问题讨论】:

    标签: ios swift layout uicollectionview uikit


    【解决方案1】:

    没有很好的文档记录,但是有 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 获取正确的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 1970-01-01
      • 2013-12-16
      • 2014-03-16
      • 2014-08-30
      相关资源
      最近更新 更多