import UIKit
class CircularViewController: UIViewController, URLSessionDownloadDelegate {
let progressLayer = CAShapeLayer()
var pulsatingLayer: CAShapeLayer!
let percentageLabel: UILabel = {
let label = UILabel()
label.text = "Start"
label.textAlignment = .center
label.font = UIFont.boldSystemFont(ofSize: 32)
return label
}()
let completionLabel: UILabel = {
let compLabel = UILabel()
compLabel.text = ""
compLabel.textAlignment = .center
compLabel.font = UIFont.boldSystemFont(ofSize: 32)
return compLabel
}()
@IBOutlet weak var imageStat:UIImageView!
@IBOutlet weak var button:UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// self.imageStat.image = #imageLiteral(resourceName: "Image-Camera")
//let center = view.center
print("test")
let trackLayer = CAShapeLayer()
let circularPath = UIBezierPath(arcCenter: .zero, radius: 100, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.lineWidth = 1
trackLayer.fillColor = nil
trackLayer.lineCap = .round
trackLayer.position = view.center
view.layer.addSublayer(trackLayer)
pulsatingLayer = CAShapeLayer()
pulsatingLayer.path = circularPath.cgPath
pulsatingLayer.strokeColor = UIColor.clear.cgColor
pulsatingLayer.lineWidth = 1
pulsatingLayer.fillColor = nil
pulsatingLayer.lineCap = .round
pulsatingLayer.position = view.center
view.layer.addSublayer(pulsatingLayer)
progressLayer.path = circularPath.cgPath
progressLayer.strokeColor = #colorLiteral(red: 0.3568627451, green: 0.1254901961, blue: 0.3098039216, alpha: 1)
progressLayer.lineWidth = 8
progressLayer.fillColor = nil
progressLayer.lineCap = .square
progressLayer.strokeEnd = 0
progressLayer.position = view.center
view.layer.addSublayer(progressLayer)
// view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
// loginButton.addTarget(self, action: #selector(ViewController.login), forControlEvents: .TouchUpInside)
view.addSubview(percentageLabel)
view.addSubview(imageStat)
view.addSubview(completionLabel)
percentageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
percentageLabel.center = view.center
imageStat.center = view.center
}
let urlString = "https://firebasestorage.googleapis.com/v0/b/firestorechat-e64ac.appspot.com/o/intermediate_training_rec.mp4?alt=media&token=e20261d0-7219-49d2-b32d-367e1606500c"
private func beginDownloadingFile() {
progressLayer.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.progressLayer.strokeEnd = percentage
}
// if (percentage == 1){
// self.imageStat.image = #imageLiteral(resourceName: "Image-Camera")
// }
print(percentage)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("Finished downloading file")
// self.imageStat.image = #imageLiteral(resourceName: "Image-Camera")
}
fileprivate func animateCircle() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.toValue = 1
basicAnimation.duration = 2
basicAnimation.fillMode = .forwards
basicAnimation.isRemovedOnCompletion = false
progressLayer.add(basicAnimation, forKey: "urSoBassic")
}
@objc private func handleTap() {
beginDownloadingFile()
// animateCircle()
}
}