CollectionView上的Hero动画库

时间:2020-06-12 15:52:49

标签: swift animation cocoapods

https://github.com/HeroTransitions/Hero

在Hero的文档中,有一个名为“用法示例2”的示例,其中的collectionview单元以级联效果淡入。但是,我似乎无法在我的项目中使用它!

他们的示例代码:

self.hero.isEnabled = true
greyView.hero.id = "skyWalker"

// collectionView is the parent view of all red cells
collectionView.hero.modifiers = [.cascade]
for cell in redCells {
    cell.hero.modifiers = [.fade, .scale(0.5)]
}

我当前的代码:

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView!.register(UINib(nibName: "MyCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCollectionViewCell")

        collectionView.delegate = self
        collectionView.dataSource = self
        navigationController?.setNavigationBarHidden(false, animated: true)

        // Hero Logic
        self.navigationController?.hero.isEnabled = true
        self.hero.isEnabled = true
        self.collectionView.hero.isEnabled = true

        NetworkingService.shared.getCollectionViewItems { (items) in
            guard let items = items else { return }
            self.items = items

            self.collectionView.reloadData()
            self.collectionView.hero.modifiers = [.cascade]
            for cell in self.collectionView.visibleCells {
                cell.hero.modifiers = [.fade, .scale(0.5)]
            }
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        let cell:MyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath)  as! MyCollectionViewCell
        let currentItem = items[indexPath.row]
        cell.nameLabel.text = currentItem.title

        cell.isHeroEnabled = true
        cell.heroID = "cardNum" + String(indexPath.section) + String(indexPath.row)
        cell.hero.modifiers = [.fade, .scale(0.5)]

        return cell
    }
}

我尝试在多个位置设置英雄ID和动画,但我的collectionview仍然没有动画。任何帮助表示赞赏!如果该库无法正常工作,我也希望获得一些反馈,以期在不使用该库的情况下重新创建此动画的方法。

1 个答案:

答案 0 :(得分:0)

Strawn,希望不要这么晚。我不知道你问了 8 个月,但你发出这个可能是因为你用 api 调用填充了你的集合视图。如果您了解如何在屏幕开始推送转换之前获取数据,您就可以达到您的目标

在视图控制器的 viewDidLoad 中:

hero.isEnabled = true
collectionView.hero.modifiers = [.cascade]

并且在您的集合视图单元格的自定义类中,可以是它的 init 或第一个数据访问函数

hero.modifiers = [.fade, .scale(0.5), .translate(x: 100)]

如果您的导航转换成功,这也将起作用 希望能帮到别人

保持安全,快乐编码。