1
2

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.

ProgressBar

Last updated at Posted at 2018-12-23

UIProgressViewへの理解を深めるために作成。

ezgif.com-video-to-gif

#ソースコード
ゲージが溜まるとラベルがcompleteになる
リセットボタンを押すと元に戻る

ViewController.swift

import UIKit

class ViewController: UIViewController {
    
    var label: UILabel! //ラベルのインスタンスを生成
    var progressView: UIProgressView! //プログレスビューのインスタンスを生成
    var progress: Float = 0.0 // 時間計測用の変数 //起動してからMaxになるまでの時間
    var timer: Timer! //タイマー
    var restartButton: UIButton! //ボタンのインスタンス生成
    
    @objc func restart(_ s:UIButton) {
        
        timer.invalidate() //タイマーを破棄する
        progress = 0
        label.text = "prease wait・・・"
        //タイマーを作る 0.01秒ごとにtimerUpdate)()を呼び出す
        timer = Timer.scheduledTimer(timeInterval: 0.01,target: self,selector: #selector(ViewController.timerUpdate),userInfo: nil,repeats: true)
        // リセット時にアニメーションしたくないときはコメントアウトを解除
        //progressView.setProgress(progress, animated: false)
    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //ラベル UISCrrenでサイズを取得する
        label = UILabel(frame: CGRect(x: 0, y: 0, width:UIScreen.main.bounds.width - 50, height: 20))
        var labelCenterPos = view.center
        labelCenterPos.y = labelCenterPos.y + 30
        label.center = labelCenterPos
        label.text  = "prease wait・・・"
        //テキストの位置
        label.textAlignment = .center
        view.addSubview(label)
        
        //プログレスバー
        progressView = UIProgressView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 60, height: 20))
        progressView.center = view.center
        //スケール行列を生成
        progressView.transform = CGAffineTransform(scaleX: 1.0, y: 6.0)
        // 色合い
        progressView.progressTintColor = .blue
        // アニメーションを付ける.
        progressView.setProgress(progress, animated: true)
        view.addSubview(progressView)//ビューに表示
        
        //再開ボタン
        restartButton = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 40))
        var buttonCenterPos = view.center
        buttonCenterPos.y = UIScreen.main.bounds.height - 100
        restartButton.center = buttonCenterPos
        restartButton.setTitle("restart", for: .normal) //ボタンのタイトル
        restartButton.setTitleColor(.blue, for: .normal)//通常時の色
        restartButton.setTitleColor(.red, for: .highlighted)//選択時の色
        restartButton.addTarget(self, action: #selector(ViewController.restart(_:)), for: .touchUpInside) ////ボタンをタップした際に反応するメソッドを用意
        view.addSubview(restartButton) //ビューに表示
        
        //タイマー
        timer = Timer.scheduledTimer(timeInterval: 0.01,target:self,selector:#selector(ViewController.timerUpdate), userInfo: nil, repeats: true)
        
    }
    
    @objc func timerUpdate() {
        progress = progress + 0.001
        if progress < 1.1 {
            progressView.setProgress(progress, animated: true)
        }else{
            timer.invalidate() //タイマーを破棄する
            label.text = "Complete!"
        }
    }


}

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?