Advertisement
Guest User

Untitled

a guest
May 24th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // CircularLoaderLBTA
  4. //
  5. // Created by Brian Voong on 12/8/17.
  6. // Copyright © 2017 Lets Build That App. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController, URLSessionDownloadDelegate {
  12.  
  13. let shapeLayer = CAShapeLayer()
  14.  
  15. let percentageLabel: UILabel = {
  16. let label = UILabel()
  17. label.text = "Start"
  18. label.textAlignment = .center
  19. label.font = UIFont.boldSystemFont(ofSize: 32)
  20. return label
  21. }()
  22.  
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25.  
  26. view.addSubview(percentageLabel)
  27. percentageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
  28. percentageLabel.center = view.center
  29.  
  30. // let's start by drawing a circle somehow
  31.  
  32. // let center = view.center
  33.  
  34. // create my track layer
  35. let trackLayer = CAShapeLayer()
  36.  
  37. let circularPath = UIBezierPath(arcCenter: .zero, radius: 100, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
  38. trackLayer.path = circularPath.cgPath
  39.  
  40. trackLayer.strokeColor = UIColor.lightGray.cgColor
  41. trackLayer.lineWidth = 10
  42. trackLayer.fillColor = UIColor.clear.cgColor
  43. trackLayer.lineCap = kCALineCapRound
  44. trackLayer.position = view.center
  45.  
  46. view.layer.addSublayer(trackLayer)
  47.  
  48. shapeLayer.path = circularPath.cgPath
  49.  
  50. shapeLayer.strokeColor = UIColor.red.cgColor
  51. shapeLayer.lineWidth = 10
  52. shapeLayer.fillColor = UIColor.clear.cgColor
  53. shapeLayer.lineCap = kCALineCapRound
  54. shapeLayer.position = view.center
  55.  
  56. shapeLayer.transform = CATransform3DMakeRotation(-CGFloat.pi / 2, 0, 0, 1)
  57.  
  58. shapeLayer.strokeEnd = 0
  59.  
  60. view.layer.addSublayer(shapeLayer)
  61.  
  62. view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))
  63. }
  64.  
  65. 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"
  66.  
  67. private func beginDownloadingFile() {
  68. print("Attempting to download file")
  69.  
  70. shapeLayer.strokeEnd = 0
  71.  
  72. let configuration = URLSessionConfiguration.default
  73. let operationQueue = OperationQueue()
  74. let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)
  75.  
  76. guard let url = URL(string: urlString) else { return }
  77. let downloadTask = urlSession.downloadTask(with: url)
  78. downloadTask.resume()
  79. }
  80.  
  81. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
  82. let percentage = CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)
  83.  
  84. DispatchQueue.main.async {
  85. self.percentageLabel.text = "\(Int(percentage * 100))%"
  86. self.shapeLayer.strokeEnd = percentage
  87. }
  88.  
  89. print(percentage)
  90. }
  91.  
  92. func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
  93. print("Finished downloading file")
  94. }
  95.  
  96. fileprivate func animateCircle() {
  97. let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
  98.  
  99. basicAnimation.toValue = 1
  100.  
  101. basicAnimation.duration = 2
  102.  
  103. basicAnimation.fillMode = kCAFillModeForwards
  104. basicAnimation.isRemovedOnCompletion = false
  105.  
  106. shapeLayer.add(basicAnimation, forKey: "urSoBasic")
  107. }
  108.  
  109. @objc private func handleTap() {
  110. print("Attempting to animate stroke")
  111.  
  112. beginDownloadingFile()
  113.  
  114. // animateCircle()
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement