LoginSignup
7
1

More than 5 years have passed since last update.

【iOS】ロード画面を作成する!

Last updated at Posted at 2017-12-10

ロード画面を作成するには??

通信などの重い処理がはしるときに表示するローディング画面を作成します!

実装内容

・viewを重ねて画面を少し暗くする
・くるくるまわるインジケータを表示する

スクリーンショット 2017-12-10 22.54.52.png

実装コード

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()
        }
    }

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1