0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-10-22

概要

先に結論から言うと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て便利っていう記事だったかも。

0
3
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
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?