IndicatorViewのよしなな使い方
Overview
IndicatorViewをよし何使う方法を検討してみました。
SVProgressHUD は便利にできていて、UIWindowを作成してその上で、インジケーターをくるくる回すので、
インジケーター表示させてる間はその下のViewやButtonを押下できなくなっております。
しかし、他社とさをつけるためには、別のインジケーターを表示させたほうが、良いと考えるケースも存在しております。
確かに良さげで、バリエーションが多数ある、OSSもありますが、
UIWindowなどを使っていないので、表示させてもUIButtonなどがある画面の場合押せてしまいます。
なので押せない方法で実装してみました。
とりあえず NVActivityIndicatorViewを使ってみました。
Xcodeで新規プロジェクトを作成する
Installation
Cocoapods
Install Cocoapods if need be.
$ gem install cocoapods
Add NVActivityIndicatorView
in your Podfile
.
use_frameworks!
pod 'NVActivityIndicatorView'
Then, run the following command.
$ pod install
新規プロジェクトに組み込む
ViewController.swift
インジケーター呼び出し側は下記になります。
import UIKit
class ViewController: UIViewController {
// 押せなくなっているか確認するためのボタン
@IBOutlet weak var tapButton:UIButton!
var indicatorVc:IndicatorViewController?
override func viewDidLoad() {
super.viewDidLoad()
}
// 押せるかの確認用のボタン押下した時に アラートビューを表示させる
@IBAction func pushTest(_ sender: Any) {
let alert = UIAlertController(title: "TEST", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default) { (_) in
print ("PUSH 😱")
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
// インジケーターを表示させる関数
@IBAction func showIndicator(_ sender: Any) {
self.indicatorVc = self.storyboard?.instantiateViewController(withIdentifier: IndicatorViewController.identifer) as! IndicatorViewController
indicatorVc?.modalPresentationStyle = .overCurrentContext
self.present(self.indicatorVc!, animated: false, completion: nil)
self.perform(#selector(ViewController.dismissVc), with: nil, afterDelay: 3.5)
}
@objc func dismissVc (){
self.indicatorVc?.dismiss(animated: false, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
IndicatorViewController.swift
import UIKit
import NVActivityIndicatorView
class IndicatorViewController: UIViewController{
static let identifer = "IndicatorViewController"
@IBOutlet weak var baseView: UIView!
var activity: NVActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
self.activityIndicator()
}
func activityIndicator(){
self.activity = NVActivityIndicatorView(frame: CGRect(x:0,
y:0,
width:60,
height:40),
type: NVActivityIndicatorType(rawValue: 14)!)
self.activity.center = self.view.center
self.activity.color = .black
self.view.addSubview(self.activity)
self.activity.startAnimating()
}
}
これで、インジケーター表示させた時の画面にボタンがあっても、押せなくなる、