平滑和加速ProgressView

时间:2016-05-08 03:21:27

标签: ios swift uiviewcontroller uiviewanimation uiprogressview

我在玩ProgressView。我想要实现的是在点击按钮时停止在33%,66%,100%检查点。如果我使用progressView.progress = 0.33,它可以正常工作,但它会直接到达检查点。相反,让它平稳和加速看起来会很好。

我认为animateWithDuration会起作用,但遗憾的是它没有。经过一些阅读,我发现我可以用NSTimer做点什么,但我无法实现它。

var progressView: UIProgressView?

override func viewDidLoad()
{
    super.viewDidLoad()

    progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
    progressView?.center = self.view.center

    progressView?.trackTintColor = UIColor.redColor()
    progressView?.progressTintColor = UIColor.greenColor()

    view.addSubview(progressView!)

    progressView!.progress = 0.0
}

Using this answer,我实现了每秒钟移动一次,但是我怎样才能让它在开始时变得平滑,缓慢,加速,所以看起来不错。

1 个答案:

答案 0 :(得分:1)

使用setProgress方法并将动画设置为true可使动画生效。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var pb: UIProgressView!
@IBOutlet weak var btn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,  typically from a nib.
    pb.progress = 0.00
}
@IBAction func btnPress(sender: UIButton) {

    UIView.animateWithDuration(3, animations: {
        self.pb.setProgress((self.pb.progress + 0.33), animated: true)
        }, completion: nil)
}

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

}