如何解决带有 collectionView 的 Hero ViewController 动画 - swift
每当我点击具有 collection view cell
效果的 zoom
时,我都会尝试呈现一个视图控制器。
据我所知,Hero 框架使用 HeroID
工作,您在 fromView
和 toView
中设置相同的 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
中点击并且在视图控制器的视图中?
解决方法
我认为你必须在 cell 被重用和 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,并且只为存在之前使用 value 选择的单元格设置 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)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。