装载微调器为Alamofire

问题描述 投票:1回答:2

在我的应用程序中,我使用alamofire将来自web api的JSON数据解析到我的应用程序中。我想在数据加载时添加一个加载微调器。我看过:

Alamofire Loading Indicator

但这只显示上面的小网络图标。任何帮助都会很棒。

ios json swift3 alamofire
2个回答
2
投票

您正在寻找活动指标视图UIActivityIndicatorView。必须手动添加。您可以通过故事板添加它并启用hidesWhenStopped,以便停止时指示器不可见。

在任何情况下,您必须在开始请求之前手动调用startAnimating,然后在请求完成时手动调用stopAnimating

要以编程方式执行所有操作,它看起来像这样:

// Assuming in the view controller
        let activityIndicator = UIActivityIndicatorView(style: .gray) // Create the activity indicator
        view.addSubview(activityIndicator) // add it as a  subview
        activityIndicator.center = CGPoint(x: view.frame.size.width*0.5, y: view.frame.size.height*0.5) // put in the middle
        activityIndicator.startAnimating() // Start animating
        request.perform { data, error in
            activityIndicator.stopAnimating() // On response stop animating
            activityIndicator.removeFromSuperview() // remove the view
            // ... process data
        }

0
投票

回答太晚了,但如果您仍然需要它可能会有所帮助。必须手动添加活动指示器。

UIActivityIndicator方法开始之前添加Alamofire.request

//start the activityindicator here
UIActivityIndicator.startAnimating()

//request Alamofire
Alamofire.request
{
    //parse the response and do all the math
    //end your animator here - make sure this happens on the main thread
    UIActivityIndicator.stop()
}

希望这对你有所帮助。解决方案中没有代码。

© www.soinside.com 2019 - 2024. All rights reserved.