10.UIProgressView

UIProgressView

UIProgressView看名字就知道這是一個進度條了, 使用非常簡單, 下面我們學習如何使用它

1. UIProgressView創建

let progressView = UIProgressView()
progressView.progressViewStyle = .Default
progressView.frame.size.width = 200
progressView.progress = 0.2
progressView.center = self.view.center
progressView.progressTintColor = UIColor.blackColor()
progressView.trackTintColor = UIColor.redColor()
self.view.addSubview(progressView)

運行程序
UIProgressView創建
我們可以看見, 進度條的進度顏色是黑色,剩餘進度顏色是紅色

2. UIProgressView設置進度

查看UIProgressView的定義, 我們可以發現有這麼一個方法:

@available(iOS 5.0, *)
public func setProgress(progress: Float, animated: Bool)
第一個參數是要設置的進度
第二個參數是是否需要動畫

我們來玩玩這個方法,將進度條設置爲90%

progressView.setProgress(0.9, animated: true)

運行程序
UIProgressView設置進度
我們可以看見確實有一個動畫效果

3. UIProgressView其它屬性

查看UIProgressView的定義, 發現還有幾個屬性

@available(iOS 5.0, *)
public var progressImage: UIImage?
@available(iOS 5.0, *)
public var trackImage: UIImage?

這個和設置進度條的顏色類似,只是變成了圖片而已

還有一個progressViewStyle

public var progressViewStyle: UIProgressViewStyle // default is UIProgressViewStyleDefault

我們查看一下UIProgressViewStyle:

public enum UIProgressViewStyle : Int {

    case Default // normal progress bar
    case Bar // for use in a toolbar
}

非常明瞭了:

progressViewStyle默認值是UIProgressViewStyle.Default
而UIProgressViewStyle.Bar在一個toolbar上使用

4. 完整代碼

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        let progressView = UIProgressView()
        progressView.progressViewStyle = .Default
        progressView.frame.size.width = 200
        progressView.progress = 0.2
        progressView.center = self.view.center
        progressView.progressTintColor = UIColor.blackColor()
        progressView.trackTintColor = UIColor.redColor()
        self.view.addSubview(progressView)

        progressView.setProgress(0.9, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章