1. 新增專案
2. 新增 Progress 元件
3. 為顯示結果,亦拖曳 Label
4. 新增 Button 開啟 Progress
5. Label
6. Progress
7. Button
8. 完整程式
//
// ViewController.swift
// ProgressDemo
//
// Created by Elvis Meng on 2015/10/18.
// Copyright © 2015年 Elvis Meng. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var time:Float = 0.0
var timer = NSTimer()
@IBOutlet weak var showProgress: UILabel!
@IBOutlet weak var progress: UIProgressView!
@IBOutlet weak var buttonStatus: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
progress.frame.size.width = 200
progress.progressTintColor = UIColor.redColor()
progress.trackTintColor = UIColor.greenColor()
progress.progress = Float(0)
}
@IBAction func startProgress(sender: UIButton) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1,
target: self,
selector:Selector("setProgress"),
userInfo: nil,
repeats: true)
buttonStatus.enabled = false
}
func setProgress() {
time += 1.0
progress.progress = time / 100
showProgress.text = "\(time)%"
if time >= 100 {
timer.invalidate()
buttonStatus.enabled = true
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
9. 測試
後記:
Progress 這專案中,timer 的設定是關鍵,除此要知道目前現況時,就要設定類別為UIProgressView的 .progress 這屬性。
參考:
1. Progress View Tutorialin iOS8 with Swift , http://www.ioscreator.com/tutorials/progress-view-tutorial-in-ios8-with-swift
2. Swift Demo – Add Progress Bar, http://rshankar.com/swift-demo-add-progress-bar/
3. Swift-Creating a Progress Bar, http://stackoverflow.com/questions/31432777/swift-creating-a-progress-bar
prettyprint
2015年10月20日 星期二
Progress Demo
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言