Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // CircularLoaderLBTA
- //
- // Created by Brian Voong on 12/8/17.
- // Copyright © 2017 Lets Build That App. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController, URLSessionDownloadDelegate {
- let shapeLayer = CAShapeLayer()
- let percentageLabel: UILabel = {
- let label = UILabel()
- label.text = "Start"
- label.textAlignment = .center
- label.font = UIFont.boldSystemFont(ofSize: 32)
- return label
- }()
- override func viewDidLoad() {
- super.viewDidLoad()
- view.addSubview(percentageLabel)
- percentageLabel.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
- percentageLabel.center = view.center
- // let's start by drawing a circle somehow
- // let center = view.center
- // create my track layer
- 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.lightGray.cgColor
- trackLayer.lineWidth = 10
- trackLayer.fillColor = UIColor.clear.cgColor
- trackLayer.lineCap = kCALineCapRound
- trackLayer.position = view.center
- view.layer.addSublayer(trackLayer)
- shapeLayer.path = circularPath.cgPath
- shapeLayer.strokeColor = UIColor.red.cgColor
- shapeLayer.lineWidth = 10
- shapeLayer.fillColor = UIColor.clear.cgColor
- shapeLayer.lineCap = kCALineCapRound
- 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 = "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() {
- 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
- }
- print(percentage)
- }
- func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
- print("Finished downloading file")
- }
- fileprivate func animateCircle() {
- let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
- basicAnimation.toValue = 1
- basicAnimation.duration = 2
- basicAnimation.fillMode = kCAFillModeForwards
- basicAnimation.isRemovedOnCompletion = false
- shapeLayer.add(basicAnimation, forKey: "urSoBasic")
- }
- @objc private func handleTap() {
- print("Attempting to animate stroke")
- beginDownloadingFile()
- // animateCircle()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement