參考資料:hackingwithswift.com
- @IBOutlet weak var btnStart: UIButton!
- @IBOutlet weak var progress: UIProgressView!
- @IBOutlet weak var labelMsg: UILabel!
- //計時器
- var timer: Timer?
- //計數
- var count : Int = 0
- override func viewDidLoad() {
- super.viewDidLoad()
- progress.frame.size.width = 250
- //可以自訂progress顏色
- progress.progressTintColor = UIColor.red
- progress.trackTintColor = UIColor.darkGray
- progress.progress = 0
- }
- @IBAction func donloadClick(_ sender: UIButton) {
- //按鈕失效 避免重複點擊
- btnStart.isEnabled = false
- count = 0
- //參數順序亂掉會報錯
- //The runTimedCode selector means that the timer will call a method named runTimedCode() every 0.5 seconds until the timer is terminated
- timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector:#selector(runTimedCode), userInfo: nil, repeats: true)
- }
- func runTimedCode() {
- progress.progress = Float(count) / 100
- labelMsg.text = "process: \(count)%"
- count += 1
- if count > 100 {
- timer!.invalidate() // stop
- timer = nil
- btnStart.isEnabled = true
- }
- }
2016年12月26日 星期一
[IOS] Progress View
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言