显示api调用活动指示符Swift

时间:2017-07-31 06:42:29

标签: ios swift xcode swift3

我想在执行api调用时显示活动指示符。当收到api响应时,活动指示符应该停止

但在api调用期间没有显示活动指示器我是swift的新手。这是我的代码。

import UIKit

var ARR_states : NSArray = NSArray()
var activityIndicatorView: UIActivityIndicatorView = UIActivityIndicatorView()
var VW_overlay: UIView = UIView()

class VC_search_Course: UIViewController {

@IBOutlet weak var TBL_states :UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    VW_overlay = UIView(frame: UIScreen.main.bounds)
    VW_overlay.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)

    activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    activityIndicatorView.frame = CGRect(x: 0, y: 0, width: activityIndicatorView.bounds.size.width, height: activityIndicatorView.bounds.size.height)

    activityIndicatorView.center = VW_overlay.center
    VW_overlay.addSubview(activityIndicatorView)
    VW_overlay.center = view.center
    view.addSubview(VW_overlay)

    activityIndicatorView.startAnimating()
    perform(#selector(self.Places_API), with: activityIndicatorView, afterDelay: 0.01)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}



@IBAction func button_back(sender: UIButton) {
    self.dismiss(animated: false, completion: nil)
}

func Places_API()
{
    let STR_url = String(format: "%@/search_by_location", SERVER_URL)
    let auth_tok = String(format:"%@",UserDefaults.standard.value(forKey:"auth_token") as! CVarArg)
       if let url = NSURL(string:STR_url) {
        let request = NSMutableURLRequest(url: url as URL)
        request.httpMethod = "GET"
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        request.addValue(auth_tok, forHTTPHeaderField: "auth_token")

        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in
            if error != nil {
                print("\(String(describing: error))")
                return
            }

            let temp_dictin: [AnyHashable: Any]? = ((try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [AnyHashable: Any])


            ARR_states = temp_dictin?["course_locations"] as! NSArray
            NSLog("States response %@", ARR_states)

        }
        task.resume()
    }

    activityIndicatorView.stopAnimating()
    VW_overlay.isHidden = true
}
}

以上代码未显示活动指示器我尝试更改视图背景颜色。和活动指示灯颜色但没有用

3 个答案:

答案 0 :(得分:1)

在您的响应处理程序中停止活动指示器。

func Places_API() {
activityIndicatorView.stopAnimating()
//Handle response here
}

答案 1 :(得分:1)

中删除您的代码
task.resume()
}

// activityIndicatorView.stopAnimating()
// VW_overlay.isHidden = true

并添加到块内部,在以下位置调用您的代码

 let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in

       // call here 
       activityIndicatorView.stopAnimating()
        VW_overlay.isHidden = true


        if error != nil {
            print("\(String(describing: error))")
            return
        }
       ......

答案 2 :(得分:0)

Swift 3.0

对于那我刚刚实现两个功能如下。希望这也会对你有帮助。

 func showLoader(view: UIView) -> UIActivityIndicatorView {

        //Customize as per your need
        let spinner = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height:40))
        spinner.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        spinner.layer.cornerRadius = 3.0
        spinner.clipsToBounds = true
        spinner.hidesWhenStopped = true
        spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white;
        spinner.center = view.center
        view.addSubview(spinner)
        spinner.startAnimating()
        UIApplication.shared.beginIgnoringInteractionEvents()

        return spinner
    }

并且,隐藏活动指标

extension UIActivityIndicatorView {  
     func dismissLoader() {
            self.stopAnimating()
            UIApplication.shared.endIgnoringInteractionEvents()
        } 
 }

而且,您使用指标的UIViewController如下所示。

    //Show
    let spinner = showLoader(view: self.view)

    //Hide
    spinner.dismissLoader()