0

I am trying to create a progress bar that goes from 0%-100%. When the progress reaches 100%, I would like it to fade from the percentage to an image such as a check mark. I am setting this progress bar up as a CAShapeLayer and inside of it, I would like the the image to appear after it reaches 100. I see this animation done throughout various apps and I'm wondering if it is possible to do in Xcode. I have tried to do this by converting an image to a CAShapeLayer but it fails with a compiler error:

Cannot convert value of type 'UIImage?' to expected argument type 'String'

I have used the code below, but it only shows the percentage of loading. is it possible to be done?

  private func beginDownloadingFile() {
    print("Attempting to download file")

    shapeLayer.strokeEnd = 0

    let configuration = URLSessionConfiguration.default
    let operationQueue = OperationQueue()
    let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)

    guard let url = URL(string: urlString) else { return }
    let downloadTask = urlSession.downloadTask(with: url)
    downloadTask.resume()
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    let percentage = CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)

    DispatchQueue.main.async {
        self.percentageLabel.text = "\(Int(percentage * 100))%"
        self.shapeLayer.strokeEnd = percentage
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
    }

    print(percentage)
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    print("Finished downloading file")
}

percentage label that I want to change when reached 100%

Thank you for explaining whether or not this can be accomplished in advance.

1 Answers1

0

First, the reason for getting the error message Cannot convert value of type 'UIImage?' to expected argument type 'String':

This is because in the last line (UIImage(named: tick)) in your snippet you try to initialize an UIImage with the UIImage constant tick you initialized two lines earlier.


What you try to accomplish is possible, but the way you try it will not work. To help you with that, you should show how your UI is set up.

A possible solution: Embed your percentageLabel in a UIStackView. Add a UIImageView to the stackview too. Set this ImageView to show the desired image and set the isHidden property of the imageView to true. When your progress reaches 100%, do the following:

percentageLabel.isHidden = true
imageView.isHidden = false
Robin Schmidt
  • 1,153
  • 8
  • 15
  • thank you for your reply. I removed the tick image because It wasn't working how I wanted it to. I have updated my code above and added a picture to show more of what I mean. I would really appreciate it if you could help me with this! –  Nov 26 '18 at 22:35