gpt4 book ai didi

ios - 滚动时 Swift UICollectionViewCell 动画 View 随机空白

转载 作者:搜寻专家 更新时间:2023-10-31 21:47:23 24 4
gpt4 key购买 nike

我正在尝试使用 UICollectionView 来显示一个正方形 MyCollectionViewCell,它在 UIImageView 中包含动画 GIF。

行和节的设置如下:

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 50
}

我正在使用 SwiftGif获取分配给单元格的 UIImageView 的 GIF,如下所示:

let gifs = [UIImage.gifWithName("gif1"), UIImage.gifWithName("gif2"), UIImage.gifWithName("gif3")]

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gifCell", forIndexPath: indexPath) as! GifCollectionViewCell
cell.gifImageView.image = gifs[indexPath.item % gifs.count]
return cell
}

大部分情况下一切都很好。但我的问题是,滚动时,有时单元格为空白并且没有 GIF 出现。在调试过程中,我在单元格中添加了一个标签以显示 indexPath.item,如您在上面的代码中所见,以确保单元格不会被忽略并具有发现标签将始终显示 indexPath,即使单元格不显示 GIF。

我已经用常规图像进行了测试,而不是像这样:

let testImages = [UIImage(named: "testImage1"), UIImage(named: "testImage2", UIImage(named: "testImage3")]

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gifCell", forIndexPath: indexPath) as! GifCollectionViewCell
cell.gifImageView.image = testImages[indexPath.item % testImages.count]
return cell
}

并且没有出现空白单元格。

更好奇的是,当我最初在 collectionView(...cellForItemAtIndexPath) 中构建 GIF 时,我也没有遇到任何空白单元格问题:

let gifNames = ["gif1", "gif2", "gif3"]

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gifCell", forIndexPath: indexPath) as! GifCollectionViewCell
let gif = UIImage.gifWithName(gifNames[indexPath.item % gifNames.count])
cell.gifImageView.image = gif
return cell
}

如果不是因为 GIF 构建过程严重影响 UICollectionView 的滚动性能,这个最初的实现会起作用,这首先迫使我改变实现。

我已经确认这不是 SwiftGif 的问题,因为我已经在另一个应用程序中使用动画渲染库和 AnimatedView 代替 UIImageView 复制了这个问题> 在 MyCollectionViewCell 中显示动画而不是 GIF,并且在滚动 CollectionView 时出现相同的问题,单元格随机不显示任何内容而不是动画。

我已经尝试了 StackOverflow 解决方案 here并像这样实现了自定义 UICollectionViewFlowLayout:

class MyFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElementsInRect(rect)
let contentSize = self.collectionViewContentSize()
let newAttrs = attributes?.filter { $0.frame.maxX <= contentSize.width && $0.frame.maxY <= contentSize.height}
return newAttrs
}
}

并在 viewDidLoad() 中分配它,如下所示:

self.collectionView?.collectionViewLayout = MyFlowLayout()

MyFlowLayout 我也尝试过:

override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}

我还弄乱了各种单元格大小(宽度 1 小于高度,高度 1 小于宽度等)并弄乱了一些部分插图组合,但未能找到导致动画 View 的问题根源不要出现。

如有任何帮助,我们将不胜感激。谢谢

最佳答案

我通过在分配图像之前在 collectionView(...cellForItemAtIndexPath) 中设置 gifImageView.image = nil 解决了这个问题。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("gifCell", forIndexPath: indexPath) as! GifCollectionViewCell
cell.gifImageView.image = nil // Added this line
cell.gifImageView.image = gifs[indexPath.item % gifs.count]
cell.infoLabel.text = String(indexPath.item)
return cell
}

不确定如何/为什么修复它,但它现在可以工作了。

关于ios - 滚动时 Swift UICollectionViewCell 动画 View 随机空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39713896/

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