导入UIKit
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(目标:自我,动作:#selector(handleTap))) button.addTarget(self,action:#selector(handleTap),用于:.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
}
//如果(百分比== 1){ // self.imageStat.image = #imageLiteral(resourceName:“ Image-Camera”) //} 打印(百分比) }
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()
}
}
答案 0 :(得分:1)
这就是它为我工作的方式:
func connectToAnOpenTokSession()
public func setProgressNetwork(to progressConstant: Double, withAnimation: Bool)
一起调用。对于预呼叫,您必须选择其他会话ID。
//ProgressViewController
func connectToAnOpenTokSession() {
session = OTSession(apiKey: kApiKey, sessionId: kSessionId, delegate: self as OTSessionDelegate)
var error: OTError?
session?.connect(withToken: kToken, error: &error)
if error != nil {
print(error!)}
}
//TestViewController
@objc func handleTapNetwork() {
progressBar.connectToAnOpenTokSession()
progressBar.safePercent = 100
progressBar.setProgressNetwork(to: 1, withAnimation: true)}
//TestViewController
@IBAction func networkButtonAction(_ sender: UIButton) {
networkButton.addTarget(self, action: #selector(handleTapNetwork), for:
.primaryActionTriggered) }