在Swift中加载图像之前,UIActivityIndi​​cator消失了

时间:2017-08-11 06:54:59

标签: ios swift xcode uiactivityindicatorview activity-indicator

我有这样的代码

startAnimating(CGSize(width: 100, height: 100), message: "Loading...", type: NVActivityIndicatorType.pacman, displayTimeThreshold: 5, minimumDisplayTime: 5)
    self.view.makeToastActivity(.center)
    DispatchQueue.global(qos: .background).async {            
        //self.view.addSubview(collectionNews)
        //activityIndicatorView.stopAnimating()
         DispatchQueue.main.async {
            self.myGroup.enter()
            self.view.isUserInteractionEnabled = false
            self.view.window?.isUserInteractionEnabled = false
            self.getJsonFromUrl()
            self.collectionNews.dataSource = self
            self.collectionNews.delegate = self
            self.collectionNews.contentInset = UIEdgeInsetsMake(0, 5, 0, 5)
            self.getJsonFromUrl2()
            self.myGroup.leave()
            self.refresh_now()
            self.myGroup.notify(queue: .main) {
                self.stopAnimating()
                self.view.hideToastActivity()
                self.view.isUserInteractionEnabled = true
                self.view.window?.isUserInteractionEnabled = true
            }
        }            
    }

但是,当我运行应用程序时,活动指示器总是在所有图像成功加载之前消失。当所有内容成功加载之前,当用户尝试滚动应用程序上的每个位置时,这将触发错误。

甚至,我使用2个活动指示器,并且在加载每个东西之前它们都会消失。

有人能给我建议吗,哪一个我做错了?

1 个答案:

答案 0 :(得分:-1)

您正在主线程中加载内容。您可以在后台从服务器加载/获取数据,并在主线程中更新UI。像:

startAnimating(CGSize(width: 100, height: 100), message: "Loading...", type: NVActivityIndicatorType.pacman, displayTimeThreshold: 5, minimumDisplayTime: 5)
    self.view.makeToastActivity(.center)
    DispatchQueue.global(qos: .background).async {            
        //self.view.addSubview(collectionNews)
        //activityIndicatorView.stopAnimating()

            self.myGroup.enter()
            self.view.isUserInteractionEnabled = false
            self.view.window?.isUserInteractionEnabled = false
            self.getJsonFromUrl()
            self.collectionNews.dataSource = self
            self.collectionNews.delegate = self
            self.collectionNews.contentInset = UIEdgeInsetsMake(0, 5, 0, 5)
            self.getJsonFromUrl2()
            self.myGroup.leave()
            self.refresh_now()

       DispatchQueue.main.async {
            self.myGroup.notify(queue: .main) {
                self.stopAnimating()
                self.view.hideToastActivity()
                self.view.isUserInteractionEnabled = true
                self.view.window?.isUserInteractionEnabled = true
            }
        }            
    }