TSToastTool.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // TSToastTool.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. import SVProgressHUD
  8. public let TSToastShared = TSToastTool.shared
  9. public let TSToastWindow = TSToastTool.getCurrentWindow()
  10. open class TSToastTool {
  11. public static let shared = TSToastTool()
  12. private init() {
  13. SVProgressHUD.swizzleMethods()
  14. // 如果您想要堆叠 HUD,您可以使用以下方法平衡每个节目通话:
  15. // [SVProgressHUD popActivity];
  16. SVProgressHUD.setDefaultStyle(.dark)
  17. SVProgressHUD.setDefaultAnimationType(.native)
  18. SVProgressHUD.setDefaultMaskType(.clear)
  19. SVProgressHUD.setMinimumSize(CGSize(width: 100, height: 100))
  20. SVProgressHUD.setCornerRadius(16.0)
  21. SVProgressHUD.setBackgroundColor("#000000".uiColor.withAlphaComponent(0.8))
  22. SVProgressHUD.setForegroundColor(.white)
  23. }
  24. public func setUIContent(font:UIFont? = nil,infoImage:UIImage? = nil) {
  25. if let font = font {
  26. SVProgressHUD.setFont(font)
  27. }
  28. if let infoImage = infoImage {
  29. SVProgressHUD.setInfoImage(infoImage)
  30. }
  31. }
  32. /// 显示文字提示
  33. public func showToast(text: String?, duration: TimeInterval = 3.0,containerView:UIView? = nil) {
  34. if let text = text {
  35. if text.count == 0 {
  36. return
  37. }
  38. kExecuteOnMainThread {
  39. if let containerView = containerView {
  40. SVProgressHUD.setContainerView(containerView)
  41. }
  42. SVProgressHUD.showInfo(withStatus: text)
  43. }
  44. }
  45. }
  46. /// 显示加载动画
  47. public func showLoading(text:String? = nil,containerView:UIView?) {
  48. kExecuteOnMainThread {
  49. // SVProgressHUD.dismiss()
  50. SVProgressHUD.setContainerView(containerView)
  51. SVProgressHUD.show(withStatus: text)
  52. }
  53. }
  54. /// 隐藏加载动画
  55. public func hideLoading() {
  56. kExecuteOnMainThread {
  57. SVProgressHUD.dismiss()
  58. }
  59. }
  60. /// 显示进度提示
  61. public func showProgress(progress:Float, status: String?,containerView:UIView?) {
  62. kExecuteOnMainThread {
  63. // if let containerView = containerView {
  64. SVProgressHUD.setContainerView(containerView)
  65. // }
  66. SVProgressHUD.showProgress(progress, status: status)
  67. }
  68. }
  69. /// 获取当前窗口,兼容 iOS 13 及以上版本
  70. static func getCurrentWindow() -> UIWindow? {
  71. if #available(iOS 13.0, *) {
  72. // iOS 13 及以上使用 `scene` 获取当前 window
  73. guard let scene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene else {
  74. return UIApplication.shared.keyWindow
  75. }
  76. return scene.windows.first { $0.isKeyWindow }
  77. } else {
  78. // iOS 12 及以下直接从 keyWindow 获取
  79. return UIApplication.shared.keyWindow
  80. }
  81. }
  82. }
  83. public func kShowToastDataMissing(){
  84. TSToastShared.showToast(text: "Data missing")
  85. }