UIActivityIndicatorView
[swift]UIActivityIndicatorView(ロード時のくるくる)を使う方法
[環境]
Xcode:7.3.1
Swift:2.2
ロード中に画面上でくるくる回るこれ(インジケーター)を使うための設定方法です。

例1:下記の場合のインジケーターを設定してみましょう。
————————
frame:100×100(角丸にする)
style(color):White
backgroundColor:Black
position:自分で設定した位置
※アニメーションが終了すると自動的にインジケーターを非表示にする。
————————
まず、インジケーターを定義します。
1 2 3 4 5 |
class testViewController: UIViewController { var indicator: UIActivityIndicatorView! 〜省略〜 |
今回はインジケーターのスタートとストップのfunctionを作る形にしてみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
func startIndicator() { // UIActivityIndicatorViewを生成 indicator = UIActivityIndicatorView() // 以下、各種プロパティ設定 // indicatorのframeを作成 indicator.frame = CGRectMake(0, 0, 100, 100) // frameを角丸にする場合は数値調整 indicator.layer.cornerRadius = 8 // indicatorのstyle(color)を設定 indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White // indicatorのbackgroundColorを設定 indicator.backgroundColor = UIColor.blackColor() // indicatorの配置を設定 indicator.layer.position = CGPoint(x: self.view.bounds.width/2, y: 160) // indicatorのアニメーションが終了したら自動的にindicatorを非表示にするか否かの設定 indicator.hidesWhenStopped = true // indicatorのアニメーションを開始 indicator.startAnimating() // viewにindicatorを追加 self.view.addSubview(indicator) } |
1 2 3 4 5 6 7 8 9 |
func stopIndicator() { // indicatorのアニメーションを終了 indicator.stopAnimating() // [indicator.hidesWhenStopped = false]としている場合は、 // [indicator.removeFromSuperview()]などでindicatorを手動で非表示にしてね。 } |
例2:下記の場合のインジケーターを設定してみましょう。
————————
frame:viewの横幅・高さいっぱい(角丸にしない)
style(color):自分で設定したオリジナルカラー
backgroundColor:ホワイト
position:center
※アニメーションが終了すると自動的にインジケーターを非表示にする。
————————
※この場合は、インジケーターが表示されている間は、viewは白い画面(になっているように見え、)インジケーターがviewの中央でアニメーションするような形になります。
まず、インジケーターを定義するところまでは上記同様です。
functionの内容が変わります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
func startIndicator() { // UIActivityIndicatorViewを生成 indicator = UIActivityIndicatorView() // 以下、各種プロパティ設定 // indicatorのframeを作成 indicator.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height) // indicatorのstyle(color)を設定 indicator.activityIndicatorViewStyle = UIColor(red: 0.15, green: 0.73, blue: 0.13, alpha: 1) // indicatorのbackgroundColorを設定 indicator.backgroundColor = UIColor.whiteColor() // indicatorの配置を設定 indicator.center = self.view.center // indicatorのアニメーションが終了したら自動的にindicatorを非表示にするか否かの設定 indicator.hidesWhenStopped = true // indicatorのアニメーションを開始 indicator.startAnimating() // viewにindicatorを追加 self.view.addSubview(indicator) } |
1 2 3 4 5 6 7 8 9 |
func stopIndicator() { // indicatorのアニメーションを終了 indicator.stopAnimating() // [indicator.hidesWhenStopped = false]としている場合は、 // [indicator.removeFromSuperview()]などでindicatorを手動で非表示にしてね。 } |
あとは、インジケーターをスタートさせたい箇所で”startIndicator()”を呼び出し、
ストップさせたい箇所で”stoptIndicator()”を呼び出す形になります。