带 collectionView 的英雄 ViewController 动画 - swift

Hero ViewController animation with collectionView - swift

每当我点击具有 zoom 效果的 collection view cell 时,我都会尝试显示一个视图控制器。

据我所知,Hero 框架使用 HeroID 工作,您在 fromViewtoView 中设置相同的 ID,框架会为你.

我是这样设置的:

viewcontroller1 中:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
    cell.heroID = "profheroid"
    let viewcontroller2 = ViewController2()
    viewcontroller2.configureController(with: arrayOfModels[indexPath.item])
    viewcontroller2.heroModalAnimationType = .zoom
    viewcontroller2.modalPresentationStyle = .fullScreen
    self.navigationController?.present(viewcontroller2, animated: true, completion: nil)
}

并在 viewcontroller2 中:

override func viewDidLoad() {
    super.viewDidLoad()
    view.heroID = "profheroid"
}

每当我点击 collectionviewCell 时都会出现问题,演示会正确进行,但我看到同时显示了很多单元格。 我认为发生这种情况是因为 cell.heroID = "profheroid" 同时应用于更多单元格。

我如何确保在显示单元格时,heroID 仅在 cell 被点击和 viewcontroller 的视图中??

我认为你必须在重复使用单元格时和呈现 VC 后清除此 heroID。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    cell.heroID = nil // or empty
}

我认为你必须在 cellForItemAt:

中将 heroID 重置为 nil
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: YourCollectionViewCell.description(), for: indexPath) as? YourCollectionViewCell else {
        return UICollectionViewCell()
    }
    cell.heroID = nil
    
    return cell
}

将所有可见单元格的 heroID 重置为 nil,并且只为使用当前值之前选择的单元格设置 heroID:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.visibleCells.forEach { cell in
        cell.heroID = nil //reset heroID
    }
    guard let cell = collectionView.cellForItem(at: indexPath) else { return }

    cell.heroID = "profheroid"
    let viewcontroller2 = ViewController2()
    viewcontroller2.configureController(with: arrayOfModels[indexPath.item])
    viewcontroller2.heroModalAnimationType = .zoom
    viewcontroller2.modalPresentationStyle = .fullScreen
    self.navigationController?.present(viewcontroller2, animated: true, completion: nil)
}