PQLoadingHUB.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // PQLoadingHUB.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/6/5.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. public class PQLoadingHUBView: UIView {
  10. // gif每一帧图
  11. public var gifImages: [UIImage]?
  12. // gif播放时长
  13. public var duration: Double?
  14. public lazy var loadingImage: UIImageView = {
  15. let loadingImage = UIImageView()
  16. loadingImage.tintColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  17. return loadingImage
  18. }()
  19. override public init(frame: CGRect) {
  20. super.init(frame: frame)
  21. addSubview(loadingImage)
  22. isUserInteractionEnabled = false
  23. let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_loading", ofType: ".gif")!))
  24. if data != nil {
  25. PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { [weak self] _, images, duration in
  26. self?.gifImages = images
  27. self?.duration = duration
  28. }
  29. }
  30. }
  31. required init?(coder _: NSCoder) {
  32. fatalError("init(coder:) has not been implemented")
  33. }
  34. override public func layoutSubviews() {
  35. super.layoutSubviews()
  36. // 334 * 307
  37. let imageW: CGFloat = 67
  38. let imageH: CGFloat = 67
  39. loadingImage.frame = CGRect(x: (frame.width - imageW) / 2, y: (frame.height - imageW) / 2, width: imageW, height: imageH)
  40. }
  41. /// 开始加载
  42. public func loading() {
  43. loadingImage.displayGIF(data: nil, images: gifImages, repeatCount: .max, duration: duration ?? 2)
  44. }
  45. /// 停止加载
  46. public func endLoading() {
  47. loadingImage.removePlayGIF()
  48. }
  49. override public func removeFromSuperview() {
  50. loadingImage.removePlayGIF()
  51. loadingImage.removeFromSuperview()
  52. }
  53. }
  54. public class PQLoadingHUB: NSObject {
  55. public static let shared = PQLoadingHUB()
  56. public let viewTag = 11111
  57. public var isLoading: Bool = false
  58. public func showHUB(isMode:Bool = false) {
  59. DispatchQueue.main.async { [weak self] in
  60. let window = UIApplication.shared.keyWindow
  61. if (window?.viewWithTag(self!.viewTag)) == nil {
  62. let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  63. if(isMode){
  64. let backView = UIImageView.init(frame: window?.frame ?? .zero)
  65. backView.backgroundColor = .clear
  66. backView.isUserInteractionEnabled = true
  67. backView.addSubview(loadingHUB)
  68. backView.tag = self!.viewTag
  69. window?.addSubview(backView)
  70. }else{
  71. loadingHUB.tag = self!.viewTag
  72. window?.addSubview(loadingHUB)
  73. }
  74. loadingHUB.center = window?.center as! CGPoint
  75. loadingHUB.loading()
  76. self?.isLoading = true
  77. }
  78. }
  79. }
  80. public func dismissHUB() {
  81. DispatchQueue.main.async { [weak self] in
  82. let window = UIApplication.shared.keyWindow
  83. if (window?.viewWithTag(self!.viewTag)) != nil {
  84. window?.viewWithTag(self!.viewTag)?.removeFromSuperview()
  85. self?.isLoading = false
  86. }
  87. }
  88. }
  89. public func showHUB(superView: UIView, isVerticality: Bool = false) {
  90. DispatchQueue.main.async { [weak self] in
  91. if superView.viewWithTag(self!.viewTag) == nil {
  92. let hubW: CGFloat = 100
  93. let supW: CGFloat = superView.frame.width
  94. let supH: CGFloat = superView.frame.height
  95. let hubY: CGFloat = isVerticality ? ((supW - hubW) / 2) : ((supH - hubW) / 2)
  96. let hubX: CGFloat = isVerticality ? ((supH - hubW) / 2) : ((supW - hubW) / 2)
  97. let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: hubX, y: hubY, width: 100, height: 100))
  98. loadingHUB.tag = self!.viewTag
  99. superView.addSubview(loadingHUB)
  100. loadingHUB.loading()
  101. self?.isLoading = true
  102. }
  103. }
  104. }
  105. public func dismissHUB(superView: UIView) {
  106. DispatchQueue.main.async { [weak self] in
  107. if let v = superView.viewWithTag(self?.viewTag ?? 999579) {
  108. v.removeFromSuperview()
  109. self?.isLoading = false
  110. }
  111. }
  112. }
  113. override private init() {
  114. super.init()
  115. }
  116. override public func copy() -> Any {
  117. return self
  118. }
  119. override public func mutableCopy() -> Any {
  120. return self
  121. }
  122. }