Show Activity Indicator while data load in collectionView Swift

送分小仙女□ 提交于 2019-12-07 12:57:38

问题


How would I go about showing an activity Indicator and a white background while the data in my collectionView loads?

I currently have this:

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(animated)
    self.view.addSubview(activityView)
    activityView.hidesWhenStopped = true
    activityView.center = self.view.center
    activityView.startAnimating()

    DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
        fetchPosts()
    }

    DispatchQueue.main.async {
        UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
            self.collectionView?.reloadData()
            self.collectionView?.alpha = 1
            self.activityView.stopAnimating()
        }, completion: nil)
    }
}

回答1:


Try this i hope this will help you

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)

override func viewDidAppear(_ animated: Bool) {

   super.viewDidAppear(animated)

    let fadeView:UIView = UIView()
    fadeView.frame = self.view.frame
    fadeView.backgroundColor = UIColor.whiteColor()
    fadeView.alpha = 0.4

    self.view.addSubview(fadeView)

    self.view.addSubview(activityView)
    activityView.hidesWhenStopped = true
    activityView.center = self.view.center
    activityView.startAnimating()

   DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
    fetchPosts()
   }

   DispatchQueue.main.async {
    UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {
        self.collectionView?.reloadData()
        self.collectionView?.alpha = 1
        fadeView.removeFromSuperview()
        self.activityView.stopAnimating()
    }, completion: nil)
 }
}


来源:https://stackoverflow.com/questions/46650183/show-activity-indicator-while-data-load-in-collectionview-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!