ロード画面を作成するには??
通信などの重い処理がはしるときに表示するローディング画面を作成します!
実装内容
・viewを重ねて画面を少し暗くする
・くるくるまわるインジケータを表示する
実装コード
ViewController.swift
var indicatorBackgroundView: UIView!
var indicator: UIActivityIndicatorView!
//省略
func showIndicator() {
// インジケータビューの背景
indicatorBackgroundView = UIView(frame: self.view.bounds)
indicatorBackgroundView?.backgroundColor = UIColor.black
indicatorBackgroundView?.alpha = 0.4
indicatorBackgroundView?.tag = 100100
indicator: UIActivityIndicatorView! = UIActivityIndicatorView()
indicator?.activityIndicatorViewStyle = .whiteLarge
indicator?.center = self.view.center
indicator?.color = UIColor.white
// アニメーション停止と同時に隠す設定
indicator?.hidesWhenStopped = true
// 作成したviewを表示
indicatorBackgroundView?.addSubview(indicator!)
self.view.addSubview(indicatorBackgroundView!)
indicator?.startAnimating()
}
func hideIndicator(){
// viewにローディング画面が出ていれば閉じる
if let viewWithTag = self.view.viewWithTag(100100) {
viewWithTag.removeFromSuperview()
}
}