Swift UICollectionViewCell 动画视图滚动时随机空白
Swift UICollectionViewCell Animated View Randomly Blank When Scrolling
我正在尝试使用 UICollectionView
在 UIImageView
中显示具有动画 GIF 的正方形 MyCollectionViewCell
。
行和节的设置如下:
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 时遇到相同的问题,即单元格随机不显示任何内容而不是动画。
我尝试了 Whosebug 解决方案 here 并实现了自定义 UICollectionViewFlowLayout
,如下所示:
class MyFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElementsInRect(rect)
let contentSize = self.collectionViewContentSize()
let newAttrs = attributes?.filter { [=15=].frame.maxX <= contentSize.width && [=15=].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
}
不确定 how/why 这修复了它,但现在可以使用了。
我正在尝试使用 UICollectionView
在 UIImageView
中显示具有动画 GIF 的正方形 MyCollectionViewCell
。
行和节的设置如下:
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 时遇到相同的问题,即单元格随机不显示任何内容而不是动画。
我尝试了 Whosebug 解决方案 here 并实现了自定义 UICollectionViewFlowLayout
,如下所示:
class MyFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElementsInRect(rect)
let contentSize = self.collectionViewContentSize()
let newAttrs = attributes?.filter { [=15=].frame.maxX <= contentSize.width && [=15=].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
}
不确定 how/why 这修复了它,但现在可以使用了。