gpt4 book ai didi

ios - Gif用作分页加载器时,有时不显示在 ImageView 中

转载 作者:行者123 更新时间:2023-11-29 05:29:44 26 4
gpt4 key购买 nike

我正在使用带有分页的 Collection View 来显示更多数据。在 Collection View 结束时,我显示带有 gif 文件的 ImageView ,直到数据加载,然后隐藏 ImageView 。但有时 gif 无法在 ImageView 中加载。

请帮忙并提前致谢!!

我尝试过更改库 - Kingfisher、SDWebImage、ImageIO、UIImage+Gif 等,但没有帮助。还尝试在主线程上运行。

class HorizontalScrollDealCollectionView: UICollectionView {

private var indicator:UIImageView!
private var offset:CGFloat = 0
private let loaderGif = UIImage.gif(name: "831")

override func awakeFromNib() {
super.awakeFromNib()

let frame = CGRect(x: 20, y: self.frame.height/2-15, width: 30, height: 30)
indicator = UIImageView(frame: frame)
indicator.image = loaderGif
indicator.backgroundColor = .blue
self.addSubview(indicator)
indicator.isHidden = true
}

override func layoutSubviews() {
super.layoutSubviews()
self.adjustIndicator()
}

func loadGifAnimatedIndicator() {
indicator.isHidden = true
indicator.image = loaderGif
}

private func adjustIndicator() {
indicator.frame.origin.x = self.contentSize.width + (offset/2)
let view = self.visibleCells.first
if let scrollViewAdjuster = view as? InfiniteScrollOffsetAdjuster {
indicator.frame.origin.y = scrollViewAdjuster.refralFrameForProgressView.height / 2 - 15
}
else {
indicator.frame.origin.y = self.contentSize.height / 2 - 15;
}
}

func showIndicator() {
if indicator.isHidden == false { return }
indicator.isHidden = false
indicator.image = loaderGif
indicator.startAnimating()
UIView.animate(withDuration: 0.2) {
self.contentInset.right = self.offset + 40
}
}

func hideIndicator() {
if indicator.isHidden { return }
self.indicator.isHidden = true
}

func resetContentInset(animate:Bool = true) {
UIView.setAnimationsEnabled(animate)
UIView.animate(withDuration: 0.1, animations: {
self.contentInset.right = 0
}) { (success) in
if !animate {
UIView.setAnimationsEnabled(true)
}
}
}
}

在 ViewController 中

//Table View Delegate
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let homeTableViewCell = cell as? HorizontalScrollHomeTableViewCell else {
return
}
homeTableViewCell.collectionView.loadGifAnimatedIndicator()
homeTableViewCell.collectionView.reloadData()
}

//Collection View Delegate
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let horizontalCollView = collectionView as! HorizontalScrollDealCollectionView
let dataIndex = horizontalCollView.tableViewCellIndex!
let data = items[dataIndex.row]
if (indexPath.item == data.products.count - 1) && data.moreAvailable {
horizontalCollView.showIndicator()
data.loadMore(completion: {
(success) in
horizontalCollView.hideIndicator()
if success {horizontalCollView.reloadData()}
})
}
}

屏幕截图 1 Screenshot when gif works fine.

****截图2** Screenshot when gif is not loaded in imageview

最佳答案

改变

private let loaderGif = UIImage.gif(name: "831")

private var loaderGif: UIImage { 
return UIImage.gif(name: "831")
}

解决了问题,但表格 View 滚动变得迟缓/抖动。

还使用 DispatchQueue 异步执行此操作,但没有帮助。仍然滚动挂起。

func loadGifAnimatedIndicator() {
indicator.isHidden = true
DispatchQueue.main.async {
self.indicator.image = self.loaderGif
}
}

关于ios - Gif用作分页加载器时,有时不显示在 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57769992/

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