15
16

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.

【Swift4】UIProgressViewのミニマムサンプル【iOS】

Last updated at Posted at 2018-02-27

とりあえず下のコードをコピペしてビルドするだけで図のようなプログレスバーを表示することが可能です。進捗度が100%になると「please wait ...」を「Complete !」に書き換える簡単なサンプルです。

for Xcode version 9.2 beta / Swift4
iPhone_8_Plus_-_11_2.pngiPhone_8_Plus_-_11_3.png

ViewController.swift

ViewController.swift
import UIKit

class ViewController: UIViewController {

    var label:UILabel!
    var progressView:UIProgressView!
    var progress:Float = 0.0
    var timer:Timer!
    var restartButton:UIButton!
    
    @objc func restart(_ s:UIButton) {

        timer.invalidate()
        progress = 0
        label.text = "please wait ..."
        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.

        /// ラベル
        label = UILabel(frame: CGRect(x:0,y:0,width:UIScreen.main.bounds.width - 20,height:20))
        var labelCenterPos = view.center
        labelCenterPos.y = labelCenterPos.y + 30
        label.center = labelCenterPos
        label.text = "please 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 {  // 浮動小数点誤差のため、<= 1.0 だとtrueにならないことがある
            progressView.setProgress(progress, animated: true)
        } else {
            timer.invalidate()
            label.text = "Complete !"
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

進捗度はUIProgressViewのインスタンスメソッド、**setProgress()にて設定します。1.0**で100%です。

その他のミニマムサンプルシリーズ
【Swift4】UITableViewのミニマムサンプル【iOS】
【Swift4】WKWebViewのミニマムサンプル【iOS】

15
16
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
15
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?