Animate UIProgressView backward

There are situations where we need to use the UIProgressView as a count-down bar. We want to count it from 1 to 0. There are two ways in which this can be achieved.

Flipping the UIProgressView

This is the old solution that can be used if you are supporting below iOS 9.0. In this method, we create a transform to flip the progress bar 180 degrees. So basically we still go from 0 to 1 but it is from right to left giving the impression that we are counting down.

    func rotateProgressView() {
        var transform:CGAffineTransform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: countDownProgress.frame.size.height) // Flip view vertically
        transform = transform.rotated(by: .pi); //Rotation angle is in radians
        countDownProgress.transform = transform;
    }

Using semanticContentAttribute iOS 9.0 above

This is the new and cleaner approach that you can use to create a right-to-left moving progress bar. It is supported in iOS9.0 and above. We need to set semanticContentAttribute = .forceRightToLeft . The code given below will make the progress bar move from right to left.

        self.countDownProgress.semanticContentAttribute = .forceRightToLeft
        UIView.animate(withDuration: 30.0) {
            self.countDownProgress.setProgress(1.0, animated: true)
        }

A pat on the back !!