I want to get percentage of download in label. I use this code
self.percentageLabel.text = "\(Int(percentage * 100))%"
But the problem is the number showing like this ( -90043% )
Any idea ?
This my full code
import UIKit
class ViewController: UIViewController, URLSessionDownloadDelegate {
let shapeLayer = CAShapeLayer()
let percentageLabel: UILabel = {
let label = UILabel()
label.text = ""
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 32)
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.addSubview(percentageLabel)
percentageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
percentageLabel.center = view.center
let trackLayer = CAShapeLayer()
let circularPath = UIBezierPath(arcCenter: .zero, radius: 100, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.white.cgColor
trackLayer.lineWidth = 0.2
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = CAShapeLayerLineCap.round
trackLayer.position = view.center
view.layer.addSublayer(trackLayer)
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.yellow.cgColor
shapeLayer.lineWidth = 10
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineCap = CAShapeLayerLineCap.round
shapeLayer.position = view.center
shapeLayer.transform = CATransform3DMakeRotation(-CGFloat.pi/2, 0, 0, 1)
shapeLayer.strokeEnd = 0
view.layer.addSublayer(shapeLayer)
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
}
let urlString = UIPasteboard.general
private func beginDownloadingFile()
{
print("Downloading Started ... ")
self.shapeLayer.strokeEnd = 0
let configuration = URLSessionConfiguration.default
let operationQueue = OperationQueue()
let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)
if let urlString = urlString.string {
print(urlString)
guard let url = URL(string: urlString) else {
print("URL Not valid")
return
}
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Finshed Downloading Files")
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
print("TotalBytes:- ", totalBytesWritten, "and toBytesWrittenExpected:- ", totalBytesExpectedToWrite)
let percentage = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print("percentage : ", percentage)
DispatchQueue.main.async {
self.shapeLayer.strokeEnd = CGFloat(percentage)
self.percentageLabel.text = "\(Int(percentage * 100))%"
self.percentageLabel.font = UIFont.boldSystemFont(ofSize: 20)
self.percentageLabel.textColor = UIColor.white
}
print("Percentage Downloaded:- ", percentage)
}
fileprivate func animateCircle() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = 2
basicAnimation.fillMode = CAMediaTimingFillMode.forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "urSoBasic")
}
@objc private func handleTap(){
print("Do Animation")
beginDownloadingFile()
}
}