使用NSTimer的Swift进度视图

时间:2016-03-09 17:17:40

标签: swift progress-bar nstimer uiprogressview nstimeinterval

我有一个进度视图栏,我想用它来表示时间。这是我在Swift的第一个项目,我不确定如何解决这个问题。所以任何帮助/建议都将不胜感激......

(使用Xcode 7.2和Swift 2.0)

下面是我的视图控制器。触发'btnPlaySession'时,视图控制器上的内容每20秒更改一次。当计时器计数到20时,id喜欢用进度条指示这个(因此每次内容改变时进度条都会重置)。

class CreatedSessionViewController: UIViewController {

var createdSession: [YogaPose]!
var poseDuration: Double = 20.00 
var timer = NSTimer!()
var currentPoseIndex = 1

//Outlets:
@IBOutlet var poseProgressView: UIProgressView!
@IBOutlet var lblPoseCount: UILabel!
@IBOutlet var lblPoseName: UILabel!
@IBOutlet var imgPose: UIImageView!
@IBOutlet var tvDescription: UITextView!

// Do any additional setup after loading the view:
override func viewDidLoad() {
    super.viewDidLoad()

    displayFirstPoseInArray()
}

func displayFirstPoseInArray(){
    lblPoseCount.text = (String(currentPoseIndex) + "/" + String(createdSession.count))
    lblPoseName.text = createdSession[0].title
    imgPose.image = UIImage(named: String(format: "%d.jpg", (createdSession[0].id)!))
    tvDescription.text = createdSession[0].desc
}

@IBAction func btnPlaySession(sender: AnyObject) {
    timer = NSTimer.scheduledTimerWithTimeInterval(poseDuration, target: self, selector: "getNextPoseData", userInfo: nil, repeats: true)
}

func getNextPoseData(){
    if (currentPoseIndex < createdSession.count){

        setProgressBar()

        lblPoseCount.text = (String(currentPoseIndex + 1) + "/" + String(createdSession.count))
        lblPoseName.text = createdSession[currentPoseIndex].title
        imgPose.image = UIImage(named: String(format: "%d.jpg",(createdSession[currentPoseIndex].id)!))
        tvDescription.text = createdSession[currentPoseIndex].desc
        currentPoseIndex += 1
        print(currentPoseIndex)
    }
}

func setProgressBar(){


}

}

2 个答案:

答案 0 :(得分:8)

好的 - 所以如果你想让进度条每秒更新一次,那么你需要一个每秒触发一次的计时器 - 但它会执行20次,并且调用setProgressBar作为选择器而不是getNextPoseData

setProgressBar内,您需要增加类级别属性indexProgressBar,并简单地将进度条属性progress设置为1.0 / indexProgressBar

如果indexProgressBar == 20,请致电getNextPoseData,然后重置进度条

这是一个如何做到的简化版本

class ViewController: UIViewController
{
    @IBOutlet weak var progressBar: UIProgressView!
    var timer = NSTimer!()
    var poseDuration = 20
    var indexProgressBar = 0
    var currentPoseIndex = 0

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // initialise the display
        progressBar.progress = 0.0
    }


    @IBAction func cmdGo(sender: AnyObject)
    {
        // display the first pose
        getNextPoseData()

        // start the timer
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "setProgressBar", userInfo: nil, repeats: true)
    }

    func getNextPoseData()
    {
        // do next pose stuff
        currentPoseIndex += 1
        print(currentPoseIndex)
    }

    func setProgressBar()
    {
        if indexProgressBar == poseDuration
        {
            getNextPoseData()

            // reset the progress counter
            indexProgressBar = 0
        }

        // update the display
        // use poseDuration - 1 so that you display 20 steps of the the progress bar, from 0...19
        progressBar.progress = Float(indexProgressBar) / Float(poseDuration - 1)

        // increment the counter
        indexProgressBar += 1
    }
}

答案 1 :(得分:0)

对于Continues loader

func setProgress() {
    time += 0.1
    ProgressBar.setProgress(time / 3, animated: true)
    if time >= 3 {
        self.time = 0.01
        ProgressBar.progress = 0
        let color = self.downloadProgressBar.progressTintColor
        self.downloadProgressBar.progressTintColor = self.downloadProgressBar.trackTintColor
        self.downloadProgressBar.trackTintColor = color
    }