代码之家  ›  专栏  ›  技术社区  ›  diogenes

带同步API下载的Swift Hud

  •  0
  • diogenes  · 技术社区  · 7 年前

    我正在尝试在同步下载时向我的页面添加一个Hud效果。

    如果我放入VIEWDIDLOAD(),则不会显示HUD。

    如果我输入awakefromnib(),屏幕将变暗,但进程微调器不会出现。(我也尝试过视图将显示)

    我正在使用 https://github.com/rjeprasad/RappleProgressHUD .

    这在加载数据时不起作用。它只在加载数据后立即工作。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        RappleActivityIndicatorView.startAnimating()
    
       // the API function
        let a_Json = getDataJson(symbol: a)
        let b_Json = getdDataJson(symbol: b)
    
        RappleActivityIndicatorView.stopAnimating()
    
    }
    

    为API使用Swift 4.2和Alamoire_同步。

    谢谢。

    1 回复  |  直到 7 年前
        1
  •  3
  •   rmaddy    7 年前

    您需要在后台进行数据加载,以便UI可以继续运行。

    下面是您想要的一般模式:

    // start the activity indicator here
    
    DispatchQueue.global(qos: .background).async {
        // Data loading / processing here
    
        DispatchQueue.main.async {
            // stop the activity indicator here and make any other UI updates
        }
    }
    

    在您的特定情况下:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        RappleActivityIndicatorView.startAnimating()
    
        DispatchQueue.global(qos: .background).async {
            // the API function
            let a_Json = getDataJson(symbol: a)
            let b_Json = getdDataJson(symbol: b)
    
            DispatchQueue.main.async {
                RappleActivityIndicatorView.stopAnimating()
            }
        }
    }