Help us understand the problem. What is going on with this article?

処理中をお手軽表示(Swift5)

概要

先に結論から言うとUIActivityIndicatorViewを使って簡単に実現できます。
数行ですけど、使うときにいちいち思い出したりコピペも面倒だし組み込みたいよねと言うことでextension使って標準機能っぽくしちゃいます。
論よりコードなので、早速サンプルコード

サンプルコード

ViewController.swift
//
//  ViewController.swift
//  Test
//
//  Created by hats on 2019/06/18.
//  Copyright © 2019年 hats. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {


    var testView :UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        // タップ操作の準備
        let tapGetsture = UITapGestureRecognizer(target: self, action: #selector(tapAction))
        self.view.addGestureRecognizer(tapGetsture)
        tapGetsture.delegate = self


        // TestViewを作成
        testView = UIView(frame: CGRect(x: 300, y: 300, width: 200, height: 100))
        testView.backgroundColor = UIColor.blue
        self.view?.addSubview(testView)

        // 表示!
        testView.waitingOpen()
    }

    @objc func tapAction(sender: UITapGestureRecognizer) {

        // 非表示!
        testView?.waitingClose()

    }

}



extension UIView{

    /// 処理中表示
    func waitingOpen(){
        // 通信中インジケーター表示
        let wating = UIActivityIndicatorView()
        self.addSubview(wating)

        // 制約で対象のUIViewの真ん中に出す
        let parent = self
        wating.translatesAutoresizingMaskIntoConstraints = false
        wating.centerXAnchor.constraint(equalTo: parent.centerXAnchor, constant: 0.0).isActive = true
        wating.centerYAnchor.constraint(equalTo: parent.centerYAnchor, constant: 0.0).isActive = true

        // 見た目を調整して表示
        wating.style = .whiteLarge
        wating.color = .white
        wating.startAnimating()
    }

    /// 処理中非表示
    func waitingClose(){
        // UIActivityIndicatorViewを探して消す
        let watings = self.subviews.filter{ $0 is UIActivityIndicatorView }.map({ $0 as! UIActivityIndicatorView })
        for waiting in watings{
            waiting.stopAnimating()
            waiting.removeFromSuperview()
        }
    }

}

ちょっとだけ解説

表示するとUIViewに処理中でくるくる表示されてタップすると消えます。
IMG_0057.PNG

extensionを使ってUIViewにwaitingOpenとwaitingCloseという機能を追加してます。
UIViewの真ん中に決まった形式で表示されるだけの簡単機能ですが、呼び出し側がUIView.waitingOpen()とするだけで表示できるので使いやすいと思います。

どっちかというとextensionて便利っていう記事だったかも。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした