gpt4 book ai didi

swift - 在 Swift 中加载指示器

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:59 28 4
gpt4 key购买 nike

如何在 View Controller 中显示加载指示器。

我在 viewDidLoad() 中使用 Alamofire ....

    Alamofire.request(.GET, formURL, parameters: nil)
.responseJSON { (request, response, jsonResult, error) in


}

最佳答案

有不止一种方法可以做到这一点,但如果你在 View Controller 中调用 Alamofire,你可以将这些属性添加到类中:

var spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
var loadingView: UIView = UIView()

并添加两个助手,您应该根据您的应用程序进行自定义:

func showActivityIndicator() {
dispatch_async(dispatch_get_main_queue()) {
self.loadingView = UIView()
self.loadingView.frame = CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0)
self.loadingView.center = self.view.center
self.loadingView.backgroundColor = UIColor(rgba: "#444444")
self.loadingView.alpha = 0.7
self.loadingView.clipsToBounds = true
self.loadingView.layer.cornerRadius = 10

self.spinner = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
self.spinner.frame = CGRect(x: 0.0, y: 0.0, width: 80.0, height: 80.0)
self.spinner.center = CGPoint(x:self.loadingView.bounds.size.width / 2, y:self.loadingView.bounds.size.height / 2)

self.loadingView.addSubview(self.spinner)
self.view.addSubview(self.loadingView)
self.spinner.startAnimating()
}
}

func hideActivityIndicator() {
dispatch_async(dispatch_get_main_queue()) {
self.spinner.stopAnimating()
self.loadingView.removeFromSuperview()
}
}

并在需要时调用它,例如:

showActivityIndicator()
Alamofire.request(.GET, formURL, parameters: nil)
.responseJSON { (request, response, jsonResult, error) in
self.hideActivityIndicator()

}

关于swift - 在 Swift 中加载指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28959279/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com