Search code examples
iosswiftuiviewcontrolleruiviewanimationuiprogressview

Smooth and Accelerating ProgressView


I am playing with ProgressView. What I want to achieve is stopping at 33%, 66%, 100% checkpoints on button click. It's working okay if I use progressView.progress = 0.33, but it directly lands to the checkpoint. Instead, having it smooth and accelerating would look so nice.

I thought animateWithDuration would work but unfortunately it doesn't. After some reading, I found out that I can do something with NSTimer, but I couldn't achieve it.

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, I achieved making it move once in every second, but how can I make it go smooth, slow in the beginning and accelerating, so looks nice.


Solution

  • using setProgress method and setting animation to true makes animation work.

    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.
    }
    

    }