QSLLoadingView.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // QSLLoadingView.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2025/10/30.
  6. //
  7. import UIKit
  8. class QSLLoadingView: UIView {
  9. private let statusLabel = UILabel()
  10. private let progressLabel = UILabel()
  11. private let progressLayer = CAShapeLayer()
  12. private let gradientLayer = CAGradientLayer()
  13. var progress: CGFloat = 0 {
  14. didSet {
  15. updateProgress()
  16. }
  17. }
  18. var completion: (() -> Void)?
  19. override init(frame: CGRect) {
  20. super.init(frame: frame)
  21. setupView()
  22. }
  23. required init?(coder: NSCoder) {
  24. fatalError("init(coder:) has not been implemented")
  25. }
  26. private func setupView() {
  27. self.gradientBackgroundColor(color1: .hexStringColor(hexString: "#D3FFF6"), color2: .white, width: 170.rpx, height: 170.rpx, direction: .horizontal)
  28. self.layer.cornerRadius = 16.rpx
  29. statusLabel.textAlignment = .center
  30. statusLabel.textColor = .red
  31. statusLabel.font = UIFont.boldSystemFont(ofSize: 12)
  32. statusLabel.translatesAutoresizingMaskIntoConstraints = false
  33. self.addSubview(statusLabel)
  34. progressLabel.textAlignment = .center
  35. progressLabel.textColor = .black
  36. progressLabel.font = UIFont.boldSystemFont(ofSize: 20)
  37. progressLabel.translatesAutoresizingMaskIntoConstraints = false
  38. self.addSubview(progressLabel)
  39. let circularPath = UIBezierPath(arcCenter: CGPoint(x: 85.rpx, y: 85.rpx), radius: 44.rpx, startAngle: -CGFloat.pi / 2, endAngle: 3 * CGFloat.pi / 2, clockwise: true)
  40. progressLayer.path = circularPath.cgPath
  41. progressLayer.strokeColor = UIColor.gray.cgColor
  42. progressLayer.fillColor = UIColor.clear.cgColor
  43. progressLayer.lineWidth = 8
  44. progressLayer.lineCap = .round
  45. self.layer.addSublayer(progressLayer)
  46. gradientLayer.colors = [UIColor.hexStringColor(hexString: "#19D9B1").cgColor, UIColor.hexStringColor(hexString: "#19BFD9").cgColor]
  47. gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
  48. gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
  49. gradientLayer.frame = self.bounds
  50. gradientLayer.mask = progressLayer
  51. self.layer.addSublayer(gradientLayer)
  52. NSLayoutConstraint.activate([
  53. statusLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 20),
  54. statusLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
  55. progressLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
  56. progressLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor)
  57. ])
  58. }
  59. private func updateProgress() {
  60. progressLayer.strokeEnd = progress / 100
  61. progressLabel.text = "\(Int(progress))%"
  62. print("progress:::\(progress)")
  63. if progress < 40 {
  64. statusLabel.text = "匹配中"
  65. statusLabel.textColor = .hexStringColor(hexString: "#F94848")
  66. } else if progress < 99 {
  67. statusLabel.text = "处理中"
  68. statusLabel.textColor = .hexStringColor(hexString: "#4886F9")
  69. } else if progress >= 100 {
  70. statusLabel.text = "处理完成"
  71. statusLabel.textColor = .hexStringColor(hexString: "#1DBA5E")
  72. }
  73. }
  74. }