Ios Swift UICollectionViewCell动画视图在滚动时随机为空
我试图使用Ios Swift UICollectionViewCell动画视图在滚动时随机为空,ios,swift,uiimageview,uicollectionview,uicollectionviewcell,Ios,Swift,Uiimageview,Uicollectionview,Uicollectionviewcell,我试图使用UICollectionView来显示一个正方形MyCollectionViewCell,它在UIImageView中具有动画GIF 行和节的设置如下所示: override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } override func collectionView(collectionView: UICollection
UICollectionView
来显示一个正方形MyCollectionViewCell
,它在UIImageView
中具有动画GIF
行和节的设置如下所示:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 50
}
我使用将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
}
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
}
}
self.collectionView?.collectionViewLayout = MyFlowLayout()
大多数情况下,一切都很好。但我的问题是,当滚动时,有时单元格是空白的,并且没有GIF显示。在调试过程中,我向单元格添加了一个标签以显示indexPath.item
,正如您在上面的代码中所看到的,以确保单元格没有被忽略,并且发现即使单元格没有显示GIF,标签也将始终显示indexPath
我使用常规图像进行了测试,如下所示:
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
的滚动性能这一事实,这个最初的实现可能会起作用,这正是迫使我首先更改实现的原因
我已经确认这不是SWIFTGF的问题,因为我已经在不同的应用程序中使用动画渲染库和AnimatedView
代替MyCollectionViewCell
中的UIImageView
复制了这个问题,并显示了动画而不是GIF,并且在单元格中随机出现了同样的问题在CollectionView中滚动时不显示任何内容而显示动画
我尝试了StackOverflow解决方案,并实现了自定义UICollectionViewFlowLayout
如下:
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
}
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
}
}
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
}
不确定这是如何/为什么修复的,但它现在可以工作了