BFLoadingView.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // BFLoadingView.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by ak on 2022/2/17.
  6. //
  7. import BFCommonKit
  8. import BFUIKit
  9. import Kingfisher
  10. import UIKit
  11. class BFLoadingView: UIView {
  12. var cancelHandle: ((_ sender: UIButton) -> Void)?
  13. // gif每一帧图
  14. public var gifImages: [UIImage]?
  15. // gif播放时长
  16. public var duration: Double?
  17. public lazy var loadingImage: UIImageView = {
  18. let loadingImage = UIImageView()
  19. loadingImage.tintColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  20. return loadingImage
  21. }()
  22. lazy var closedBtn: UIButton = {
  23. let closedBtn = UIButton(type: .custom)
  24. closedBtn.frame = CGRect(x: 13, y: 43, width: cDefaultMargin * 3, height: cDefaultMargin * 3)
  25. closedBtn.setImage(imageInRecordScreenKit(by: "LoadingClose"), for: .normal)
  26. closedBtn.addTarget(self, action: #selector(loadHidden), for: .touchUpInside)
  27. return closedBtn
  28. }()
  29. lazy var titleL: UILabel = {
  30. let l = UILabel()
  31. l.text = "变音中 10%"
  32. l.textAlignment = .center
  33. l.textColor = UIColor.hexColor(hexadecimal: "#389AFF")
  34. l.font = UIFont.systemFont(ofSize: 16)
  35. return l
  36. }()
  37. override init(frame: CGRect) {
  38. super.init(frame: frame)
  39. backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
  40. addSubview(loadingImage)
  41. addSubview(closedBtn)
  42. addSubview(titleL)
  43. let data = try? Data(contentsOf: URL(fileURLWithPath: currentBundle()!.path(forResource: "stuckPoint_edit_loading", ofType: ".gif")!))
  44. if data != nil {
  45. PQPHAssetVideoParaseUtil.parasGIFImage(data: data!, isRenderingColor: UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)) { [weak self] _, images, duration in
  46. self?.gifImages = images
  47. self?.duration = duration
  48. }
  49. }
  50. }
  51. required init?(coder _: NSCoder) {
  52. fatalError("init(coder:) has not been implemented")
  53. }
  54. override func layoutSubviews() {
  55. super.layoutSubviews()
  56. // 334 * 307
  57. let imageW: CGFloat = 90
  58. let imageH: CGFloat = 90
  59. loadingImage.frame = CGRect(x: (frame.width - imageW) / 2, y: (frame.height - imageW) / 2, width: imageW, height: imageH)
  60. titleL.frame = CGRect(x: (cScreenWidth - 88) / 2, y: loadingImage.frame.maxY + 10, width: 88, height: 22)
  61. }
  62. /// 显示动画
  63. public func loadShow() {
  64. isHidden = false
  65. loadingImage.displayGIF(data: nil, images: gifImages, repeatCount: .max, duration: duration ?? 2)
  66. }
  67. // 隐藏动画
  68. @objc public func loadHidden() {
  69. isHidden = true
  70. loadingImage.removePlayGIF()
  71. }
  72. deinit {
  73. BFLog(message: "销毁加载中视图")
  74. }
  75. }