gpt4 book ai didi

ios - 如何同步单元格动画

转载 作者:行者123 更新时间:2023-11-30 10:25:57 25 4
gpt4 key购买 nike

我正在尝试在 UICollectionView 的每个单元格中模拟闪烁的动画。

当我启动应用程序时,它工作正常:

enter image description here

但是当我向左滚动时,每个单元格都会获得自己的动画:

enter image description here

我想要的是当我将其滚动到左侧时,它的效果就像第一张图片一样。有一段时间,我只是将这段代码放在下面来制作动画:

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
guard let cell = cell as? ShimmeringCell else { return }
UIView.animate(withDuration: 1.0, delay: 0, options: [.autoreverse, .repeat], animations: {
cell.alpha = 0.4
}, completion: nil)
}

这是完整的代码:Github

有人可以帮助我吗?

最佳答案

我找到了答案。

为了解决这个问题,我必须创建一个新的 UIView 并将其添加到 UICollectionView 的 subview 中:

    let newCollectionView: UICollectionView = {
// Here was creating a collectionView
return collection
}()

let viewExample: UIView = {
// Here was creating a new view
return view
}()

override func viewDidLoad() {
super.viewDidLoad()

newCollectionView.delegate = self
newCollectionView.dataSource = self
newCollectionView.register(ShimmeringCell.self, forCellWithReuseIdentifier: cellId)
self.view.addSubview(self.newCollectionView)
setupCollection()
}

private func setupCollection() {
// Constraints...

self.newCollectionView.addSubview(self.viewExample)
setupViewExample()
}

private func setupViewExample() {
// Constraints...
}

之后,我将动画放在这个 View 中:

    override func viewDidLoad() {
super.viewDidLoad()

newCollectionView.delegate = self
newCollectionView.dataSource = self
newCollectionView.register(ShimmeringCell.self, forCellWithReuseIdentifier: cellId)
self.view.addSubview(self.newCollectionView)
setupCollection()

self.viewExample.alpha = 0.6
UIView.animate(withDuration: 1.0, delay: 0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
self.viewExample.alpha = 0
}, completion: nil)
}

结果:

enter image description here

感谢大家,我们在世界的某个地方再见。

Github 上有此解决方案:Github

关于ios - 如何同步单元格动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60045429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com