Swift 中的加载指示器
Loading Indicator in Swift
如何在视图控制器中显示加载指示器。
我在 viewDidLoad() 中使用 Alamofire ....
Alamofire.request(.GET, formURL, parameters: nil)
.responseJSON { (request, response, jsonResult, error) in
}
有不止一种方法可以做到这一点,但如果您在视图控制器中调用 Alamofire,您可以将这些属性添加到 class:
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()
}
如何在视图控制器中显示加载指示器。
我在 viewDidLoad() 中使用 Alamofire ....
Alamofire.request(.GET, formURL, parameters: nil)
.responseJSON { (request, response, jsonResult, error) in
}
有不止一种方法可以做到这一点,但如果您在视图控制器中调用 Alamofire,您可以将这些属性添加到 class:
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()
}