Loading an “overlay” when running long tasks in iOS

前端 未结 11 948
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 16:15

What is example for loading overlay in Swift IOS application when do a long tasks. Example for loading data from remote server. I googled but not found any answer.

U

11条回答
  •  失恋的感觉
    2020-12-22 16:55

    @Ajinkya Patil answer as a reference. Swift 4.0 and newer

    This is an Extension Solution to use on all viewController without clashing.

    Create a LoadingDialog+ViewContoller.swift

    import UIKit
    
    struct ProgressDialog {
        static var alert = UIAlertController()
        static var progressView = UIProgressView()
        static var progressPoint : Float = 0{
            didSet{
                if(progressPoint == 1){
                    ProgressDialog.alert.dismiss(animated: true, completion: nil)
                }
            }
        }
    }
    extension UIViewController{
       func LoadingStart(){
            ProgressDialog.alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
        
        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.style = UIActivityIndicatorView.Style.gray
        loadingIndicator.startAnimating();
    
        ProgressDialog.alert.view.addSubview(loadingIndicator)
        present(ProgressDialog.alert, animated: true, completion: nil)
      }
    
      func LoadingStop(){
        ProgressDialog.alert.dismiss(animated: true, completion: nil)
      }
    }
    

    call the function inside ViewController anywhere you like. like so:

    self.LoadingStart()
    

    and here's how to stop the loading dialog.

    self.LoadingStop()
    

提交回复
热议问题