LNLoadingView.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // LNLoadingView.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/29.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. private weak var curLoadingView: LNLoadingView?
  11. func showLoading() {
  12. guard curLoadingView == nil else { return }
  13. runOnMain {
  14. let view = LNLoadingView()
  15. view.show()
  16. curLoadingView = view
  17. }
  18. }
  19. func dismissLoading() {
  20. guard let curLoadingView else { return }
  21. runOnMain {
  22. curLoadingView.dismiss()
  23. }
  24. }
  25. class LNLoadingView: UIView {
  26. override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. setupViews()
  29. }
  30. func show() {
  31. UIView.appKeyWindow?.addSubview(self)
  32. self.snp.makeConstraints { make in
  33. make.edges.equalToSuperview()
  34. }
  35. }
  36. func dismiss() {
  37. removeFromSuperview()
  38. }
  39. required init?(coder: NSCoder) {
  40. fatalError("init(coder:) has not been implemented")
  41. }
  42. }
  43. extension LNLoadingView {
  44. private func setupViews() {
  45. let container = UIView()
  46. container.backgroundColor = .black.withAlphaComponent(0.8)
  47. container.layer.cornerRadius = 16
  48. addSubview(container)
  49. container.snp.makeConstraints { make in
  50. make.center.equalToSuperview()
  51. }
  52. let ic = UIImageView()
  53. ic.image = .icLoading
  54. container.addSubview(ic)
  55. ic.snp.makeConstraints { make in
  56. make.centerX.equalToSuperview()
  57. make.top.equalToSuperview().offset(12)
  58. }
  59. let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
  60. rotationAnimation.fromValue = 0.0
  61. rotationAnimation.toValue = Double.pi * 2
  62. rotationAnimation.duration = 2.0
  63. rotationAnimation.repeatCount = .infinity
  64. rotationAnimation.isRemovedOnCompletion = false
  65. rotationAnimation.timingFunction = CAMediaTimingFunction(name: .linear)
  66. ic.layer.add(rotationAnimation, forKey: "rotationAnimation")
  67. let tipsLabel = UILabel()
  68. tipsLabel.text = .init(key: "A00001")
  69. tipsLabel.font = .body_m
  70. tipsLabel.textColor = .text_1
  71. container.addSubview(tipsLabel)
  72. tipsLabel.snp.makeConstraints { make in
  73. make.horizontalEdges.equalToSuperview().inset(20)
  74. make.bottom.equalToSuperview().offset(-12)
  75. make.top.equalTo(ic.snp.bottom).offset(4)
  76. }
  77. }
  78. }