BFLoadingView.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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: (() -> Void)?
  13. public lazy var loadingImage: UIImageView = {
  14. let loadingImage = UIImageView()
  15. loadingImage.image = imageInRecordScreenKit(by: "stuckPoint_edit_loading")
  16. return loadingImage
  17. }()
  18. //是否是下载音乐的模式,不发出回调不显示进度
  19. public var isDownloadMusic:Bool = false{
  20. didSet {
  21. if isDownloadMusic {
  22. titleL.text = ""
  23. }
  24. }
  25. }
  26. lazy var closedBtn: UIButton = {
  27. let closedBtn = UIButton(type: .custom)
  28. closedBtn.frame = CGRect(x: 13, y: 43, width: cDefaultMargin * 3, height: cDefaultMargin * 3)
  29. closedBtn.setImage(imageInRecordScreenKit(by: "LoadingClose"), for: .normal)
  30. closedBtn.addTarget(self, action: #selector(loadHidden), for: .touchUpInside)
  31. return closedBtn
  32. }()
  33. lazy var titleL: UILabel = {
  34. let l = UILabel()
  35. l.text = "变声中 10%"
  36. l.textAlignment = .center
  37. l.textColor = UIColor.hexColor(hexadecimal: "#389AFF")
  38. l.font = UIFont.systemFont(ofSize: 16)
  39. return l
  40. }()
  41. override init(frame: CGRect) {
  42. super.init(frame: frame)
  43. backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
  44. addSubview(loadingImage)
  45. addSubview(closedBtn)
  46. addSubview(titleL)
  47. }
  48. required init?(coder _: NSCoder) {
  49. fatalError("init(coder:) has not been implemented")
  50. }
  51. override func layoutSubviews() {
  52. super.layoutSubviews()
  53. // 334 * 307
  54. let imageW: CGFloat = 90
  55. let imageH: CGFloat = 90
  56. loadingImage.frame = CGRect(x: (frame.width - imageW) / 2, y: (frame.height - imageW) / 2, width: imageW, height: imageH)
  57. titleL.frame = CGRect(x: (cScreenWidth - 150) / 2, y: loadingImage.frame.maxY + 10, width: 150, height: 22)
  58. }
  59. /// 显示动画
  60. public func loadShow() {
  61. isHidden = false
  62. loadingImage.layer.removeAllAnimations()
  63. loadingImage.isHidden = false
  64. // 1.创建动画
  65. let rotationAnim = CABasicAnimation(keyPath: "transform.rotation.z")
  66. // 2.设置动画的属性
  67. rotationAnim.fromValue = 0
  68. rotationAnim.toValue = Double.pi * 2
  69. rotationAnim.repeatCount = MAXFLOAT
  70. rotationAnim.duration = 1
  71. // 这个属性很重要 如果不设置当页面运行到后台再次进入该页面的时候 动画会停止
  72. rotationAnim.isRemovedOnCompletion = false
  73. // 3.将动画添加到layer中
  74. loadingImage.layer.add(rotationAnim, forKey: nil)
  75. }
  76. public func removeLoading(){
  77. isHidden = true
  78. loadingImage.layer.removeAllAnimations()
  79. loadingImage.isHidden = true
  80. }
  81. // 隐藏动画
  82. @objc public func loadHidden() {
  83. removeLoading()
  84. if cancelHandle != nil,!isDownloadMusic {
  85. cancelHandle!()
  86. }
  87. }
  88. deinit {
  89. BFLog(message: "销毁加载中视图")
  90. }
  91. }