PQLoadingHUB.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // 主题色
  15. public var lodingColor: UIColor? {
  16. didSet{
  17. if lodingColor != nil {
  18. loadingImage.tintColor = lodingColor ?? UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  19. let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle.current(moduleName: "BFCommonKit")?.path(forResource: "stuckPoint_music_loading", ofType: ".gif") ?? ""))
  20. if data != nil {
  21. PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: lodingColor ?? UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { [weak self] _, images, duration in
  22. self?.gifImages = images
  23. self?.duration = duration
  24. }
  25. }
  26. }
  27. }
  28. }
  29. public lazy var loadingImage: UIImageView = {
  30. let loadingImage = UIImageView()
  31. loadingImage.tintColor = lodingColor ?? UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  32. return loadingImage
  33. }()
  34. override public init(frame: CGRect) {
  35. super.init(frame: frame)
  36. addSubview(loadingImage)
  37. isUserInteractionEnabled = false
  38. let data = try? Data(contentsOf: URL(fileURLWithPath: Bundle.current(moduleName: "BFCommonKit")?.path(forResource: "stuckPoint_music_loading", ofType: ".gif") ?? ""))
  39. if data != nil {
  40. PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: lodingColor ?? UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)) { [weak self] _, images, duration in
  41. self?.gifImages = images
  42. self?.duration = duration
  43. }
  44. }
  45. }
  46. required init?(coder _: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. override public func layoutSubviews() {
  50. super.layoutSubviews()
  51. // 334 * 307
  52. let imageW: CGFloat = 67
  53. let imageH: CGFloat = 62
  54. loadingImage.frame = CGRect(x: (frame.width - imageW) / 2, y: (frame.height - imageW) / 2, width: imageW, height: imageH)
  55. }
  56. /// 开始加载
  57. public func loading() {
  58. loadingImage.displayGIF(data: nil, images: gifImages, repeatCount: .max, duration: duration ?? 2)
  59. }
  60. /// 停止加载
  61. public func endLoading() {
  62. loadingImage.removePlayGIF()
  63. }
  64. override public func removeFromSuperview() {
  65. loadingImage.removePlayGIF()
  66. loadingImage.removeFromSuperview()
  67. }
  68. }
  69. public class PQLoadingHUB: NSObject {
  70. public static let shared = PQLoadingHUB()
  71. public let viewTag = 11111
  72. public var isLoading: Bool = false
  73. public func showHUB(isMode:Bool = false,lodingColor:UIColor? = nil) {
  74. DispatchQueue.main.async { [weak self] in
  75. let window = UIApplication.shared.keyWindow
  76. if (window?.viewWithTag(self!.viewTag)) == nil {
  77. let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  78. loadingHUB.lodingColor = lodingColor
  79. if(isMode){
  80. let backView = UIImageView.init(frame: window?.frame ?? .zero)
  81. backView.backgroundColor = .clear
  82. backView.isUserInteractionEnabled = true
  83. backView.addSubview(loadingHUB)
  84. backView.tag = self!.viewTag
  85. window?.addSubview(backView)
  86. }else{
  87. loadingHUB.tag = self!.viewTag
  88. window?.addSubview(loadingHUB)
  89. }
  90. loadingHUB.center = window?.center as! CGPoint
  91. loadingHUB.loading()
  92. self?.isLoading = true
  93. }
  94. }
  95. }
  96. public func dismissHUB() {
  97. DispatchQueue.main.async { [weak self] in
  98. let window = UIApplication.shared.keyWindow
  99. if (window?.viewWithTag(self!.viewTag)) != nil {
  100. window?.viewWithTag(self!.viewTag)?.removeFromSuperview()
  101. self?.isLoading = false
  102. }
  103. }
  104. }
  105. public func showHUB(superView: UIView, isVerticality: Bool = false,lodingColor:UIColor? = nil,topMargin:CGFloat? = nil) {
  106. DispatchQueue.main.async { [weak self] in
  107. if superView.viewWithTag(self!.viewTag) == nil {
  108. let hubW: CGFloat = 100
  109. let supW: CGFloat = superView.frame.width
  110. let supH: CGFloat = superView.frame.height
  111. let hubY: CGFloat = isVerticality ? ((supW - hubW) / 2) : ((supH - hubW) / 2)
  112. let hubX: CGFloat = isVerticality ? ((supH - hubW) / 2) : ((supW - hubW) / 2)
  113. let loadingHUB: PQLoadingHUBView = PQLoadingHUBView(frame: CGRect(x: hubX, y: topMargin != nil ? topMargin ?? 0: hubY, width: 100, height: 100))
  114. loadingHUB.lodingColor = lodingColor
  115. loadingHUB.tag = self!.viewTag
  116. superView.addSubview(loadingHUB)
  117. loadingHUB.loading()
  118. self?.isLoading = true
  119. }
  120. }
  121. }
  122. public func dismissHUB(superView: UIView) {
  123. DispatchQueue.main.async { [weak self] in
  124. if superView.viewWithTag(self!.viewTag) != nil {
  125. superView.viewWithTag(self!.viewTag)?.removeFromSuperview()
  126. self?.isLoading = false
  127. }
  128. }
  129. }
  130. override private init() {
  131. super.init()
  132. }
  133. override public func copy() -> Any {
  134. return self
  135. }
  136. override public func mutableCopy() -> Any {
  137. return self
  138. }
  139. }