【问题标题】:Hero ViewController animation with collectionView - swift带有 collectionView 的 Hero ViewController 动画 - swift
【发布时间】:2021-02-08 13:44:32
【问题描述】:

每当我点击带有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 被点击和视图控制器的视图中??

【问题讨论】:

    标签: swift uicollectionview uicollectionviewcell viewcontroller


    【解决方案1】:

    我认为你必须在重复使用单元格时以及在出现 VC 之后清除这个 heroID。

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

    【讨论】:

    • 清除,但是如何在 vc2 中获得单元格的引用以便在 vc2 出现后清除它?
    【解决方案2】:

    我认为你必须在 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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 2018-01-24
      • 1970-01-01
      • 2014-07-16
      • 2019-11-12
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      相关资源
      最近更新 更多