Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Gif用作分页加载程序时,有时不显示在图像视图中_Ios_Swift_Pagination_Uicollectionview_Gif - Fatal编程技术网

Ios Gif用作分页加载程序时,有时不显示在图像视图中

Ios Gif用作分页加载程序时,有时不显示在图像视图中,ios,swift,pagination,uicollectionview,gif,Ios,Swift,Pagination,Uicollectionview,Gif,我使用带有分页的集合视图来显示更多数据。在集合视图的末尾,我使用gif文件显示图像视图,直到加载数据,然后隐藏图像视图。但有时gif不会加载到图像视图中 请提前帮助和感谢 我尝试过改变库——翠鸟、SDWebImage、ImageIO、UIImage+Gif等,但都没用。 还尝试在主线程上运行 class HorizontalScrollDealCollectionView: UICollectionView { private var indicator:UIImageView!

我使用带有分页的集合视图来显示更多数据。在集合视图的末尾,我使用gif文件显示图像视图,直到加载数据,然后隐藏图像视图。但有时gif不会加载到图像视图中

请提前帮助和感谢

我尝试过改变库——翠鸟、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

****截图2** 改变

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

解决了该问题,但tableview滚动变得滞后/抖动

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

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

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