TSCommonloadingView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // TSCommonloadingView.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/1/17.
  6. //
  7. import Kingfisher
  8. class TSCommonloadingView: TSBaseView {
  9. lazy var imageView: UIImageView = {
  10. let imageView = UIImageView.createImageView(imageName: "failed_big")
  11. imageView.isHidden = true
  12. return imageView
  13. }()
  14. lazy var animatedImageView: AnimatedImageView = {
  15. let animatedImageView = AnimatedImageView()
  16. animatedImageView.autoPlayAnimatedImage = false
  17. if let gifURL = Bundle.main.url(forResource: "rotatingAnimation", withExtension: "gif") {
  18. animatedImageView.kf.setImage(with: gifURL, options: [.cacheOriginalImage]) { result in
  19. switch result {
  20. case .success(let value):
  21. print("GIF 加载成功: \(value.source.url?.absoluteString ?? "")")
  22. case .failure(let error):
  23. print("GIF 加载失败: \(error.localizedDescription)")
  24. }
  25. }
  26. }
  27. return animatedImageView
  28. }()
  29. lazy var textLabel: UILabel = {
  30. let textLabel = UILabel.createLabel(font: .font(size: 14),textColor: .fromHex("#FFFFFF",alpha: 0.8))
  31. return textLabel
  32. }()
  33. private var targetView: UIView = UIView()
  34. override func creatUI() {
  35. contentView.addSubview(imageView)
  36. imageView.snp.makeConstraints { make in
  37. make.width.height.equalTo(200.0)
  38. make.centerX.equalToSuperview()
  39. make.top.equalToSuperview()
  40. }
  41. contentView.addSubview(animatedImageView)
  42. animatedImageView.snp.makeConstraints { make in
  43. make.width.height.equalTo(200.0)
  44. make.centerX.equalToSuperview()
  45. make.top.equalToSuperview()
  46. }
  47. contentView.addSubview(textLabel)
  48. textLabel.snp.makeConstraints { make in
  49. make.height.equalTo(18.0)
  50. make.top.equalTo(imageView.snp.bottom).offset(16)
  51. make.leading.bottom.trailing.equalToSuperview()
  52. }
  53. }
  54. override func dealThings() {
  55. // 监听应用生命周期事件
  56. NotificationCenter.default.addObserver(
  57. self,
  58. selector: #selector(handleAppDidEnterBackground),
  59. name: UIApplication.didEnterBackgroundNotification,
  60. object: nil
  61. )
  62. NotificationCenter.default.addObserver(
  63. self,
  64. selector: #selector(handleAppWillEnterForeground),
  65. name: UIApplication.willEnterForegroundNotification,
  66. object: nil
  67. )
  68. }
  69. lazy var isRotating = false{
  70. didSet{
  71. if isRotating == true{
  72. startRotating(view: imageView)
  73. }else{
  74. stopRotating(view: imageView)
  75. }
  76. }
  77. }
  78. /// 开始旋转
  79. func startRotating(view:UIView,duration: Double = 2.0) {
  80. kDelayMainShort {
  81. self.animatedImageView.startAnimating()
  82. }
  83. }
  84. /// 停止旋转
  85. func stopRotating(view:UIView) {
  86. animatedImageView.stopAnimating()
  87. }
  88. func showLoading(text:String){
  89. animatedImageView.isHidden = false
  90. imageView.isHidden = true
  91. textLabel.text = text
  92. isRotating = true
  93. }
  94. func showError(text:String){
  95. animatedImageView.isHidden = true
  96. imageView.isHidden = false
  97. imageView.image = UIImage(named: "failed_big")
  98. textLabel.text = text
  99. isRotating = false
  100. }
  101. @objc private func handleAppDidEnterBackground() {
  102. stopRotating(view: targetView)
  103. }
  104. @objc private func handleAppWillEnterForeground() {
  105. isRotating = isRotating
  106. }
  107. }