JSON読み込みとアニメーション
作り方
必要なUIViewをStoryboardで配置し、コードに接続する。
Podfile
pod 'Alamofire' pod 'SwiftyJSON'
Util.swift
import Alamofire class Util { class func jsonRequest( url: String, success: @escaping (_ data: Dictionary<String, Any>) -> Void, fail: @escaping (_ error: Error?) -> Void) { Alamofire.request(url, method: .get).responseJSON { response in if response.result.isSuccess { success(response.result.value as! Dictionary) } else { fail(response.result.error) } } } }
TestJsonViewController.swift
import UIKit import SwiftyJSON class TestJsonViewController: UIViewController { @IBOutlet weak var indicator: UIActivityIndicatorView! @IBOutlet weak var text: UITextView! @IBOutlet weak var picker: UIPickerView! @IBOutlet weak var loadingView: UIVisualEffectView! let pickerData = [ "http://ip.jsontest.com/", "http://headers.jsontest.com/", "http://date.jsontest.com", "http://echo.jsontest.com/insert-key-here/insert-value-here/key/value", "http://validate.jsontest.com/?json=[JSON-code-to-validate]", "http://cookie.jsontest.com/", "http://md5.jsontest.com/?text=[text to MD5]" ] var pickerSelect: String = "" override func viewDidLoad() { super.viewDidLoad() loadingView.isHidden = true loadingView.alpha = 0 indicator.hidesWhenStopped = true pickerSelect = pickerData[0] self.pickerInit() } @IBAction func onTouched(_ sender: Any) { loadingView.isHidden = false loadingView.alpha = 0 UIView.animate(withDuration: 0.25, animations: { self.loadingView.alpha = 1.0 }, completion: { flag in self.jsonResult(url: self.pickerSelect) }) } func jsonResult(url: String) { indicator.startAnimating() Util.jsonRequest( url: url, success: { dic in let json = JSON(dic) UIView.animate(withDuration: 0.5, animations: { self.loadingView.alpha = 0.0 }, completion: { flag in self.loadingView.isHidden = true self.indicator.stopAnimating() self.text.textColor = UIColor.black if let raw = json.rawString() { self.text.textColor = UIColor.black self.text.text = raw } else { self.text.textColor = UIColor.red self.text.text = "[JSON ERR]: jsonデータないみたいです。" } }) }, fail: { error in UIView.animate(withDuration: 0.3, animations: { self.loadingView.alpha = 0.0 }, completion: { flag in self.loadingView.isHidden = true self.indicator.stopAnimating() self.text.textColor = UIColor.red self.text.text = "[通信失敗]: " + error!.localizedDescription }) }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } // picker拡張 extension TestJsonViewController : UIPickerViewDelegate, UIPickerViewDataSource { func pickerInit() { picker.delegate = self picker.dataSource = self // picker.selectRow(1, inComponent: 0, animated: true) } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return pickerData.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return pickerData[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { pickerSelect = pickerData[row] } }