且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在collectionView Swift中加载数据时显示活动指示器

更新时间:2023-12-05 16:45:46

试试这个,希望对你有帮助

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)
 }
}