ios – Swift UICollectionViewCell动画视图在滚动时随机空白

前端之家收集整理的这篇文章主要介绍了ios – Swift UICollectionViewCell动画视图在滚动时随机空白前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用UICollectionView来显示在UI ImageView中具有动画GIF的方形MyCollectionViewCell.

行和部分设置如下:

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

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

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

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,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,forIndexPath: indexPath) as! GifCollectionViewCell
    let gif = UIImage.gifWithName(gifNames[indexPath.item % gifNames.count])
    cell.gifImageView.image = gif
    return cell
}

如果不是因为GIF构建过程对UICollectionView的滚动性能产生巨大影响,这就迫使我首先改变实现,这个原始实现会起作用.

我已经确认这不是SwiftGif的问题,因为我在一个不同的应用程序中使用动画渲染库和AnimatedView代替MyCollectionViewCell中的UIImageView复制了这个问题并显示了动画而不是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小于宽度等)并且与一些插入组合混乱,但未设法找到导致动画视图不到的此问题的来源出现.

任何帮助,将不胜感激.谢谢

解决方法

我在分配图像之前通过在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相关文章