GlobalImports.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // GlobalImports.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/20.
  6. //
  7. @_exported import Foundation
  8. @_exported import UIKit
  9. @_exported import SnapKit
  10. import AVFoundation
  11. public func k_Height_statusBar() -> CGFloat {
  12. var statusBarHeight: CGFloat = 0;
  13. if #available(iOS 13.0, *) {
  14. let scene = UIApplication.shared.connectedScenes.first;
  15. guard let windowScene = scene as? UIWindowScene else {return 0};
  16. guard let statusBarManager = windowScene.statusBarManager else {return 0};
  17. statusBarHeight = statusBarManager.statusBarFrame.height;
  18. } else {
  19. statusBarHeight = UIApplication.shared.statusBarFrame.height;
  20. }
  21. return statusBarHeight;
  22. }
  23. /// ②、顶部安全区高度 k_Height_safeAreaInsetsTop
  24. public func k_Height_safeAreaInsetsTop() -> CGFloat {
  25. if #available(iOS 13.0, *) {
  26. let scene = UIApplication.shared.connectedScenes.first;
  27. guard let windowScene = scene as? UIWindowScene else {return 0}; // guard:如果 expression 值计算为false,则执行代码块内的 guard 语句。(必须包含一个控制语句: return、 break、 continue 或 throw。)。as?:类型转换,(还有这两种:as、as!)
  28. guard let window = windowScene.windows.first else {return 0};
  29. return window.safeAreaInsets.top;
  30. } else if #available(iOS 11.0, *) {
  31. guard let window = UIApplication.shared.windows.first else {return 0};
  32. return window.safeAreaInsets.top;
  33. }
  34. return 0;
  35. }
  36. /// ③、底部安全区高度
  37. func k_Height_safeAreaInsetsBottom() -> CGFloat {
  38. if #available(iOS 13.0, *) {
  39. let scene = UIApplication.shared.connectedScenes.first;
  40. guard let windowScene = scene as? UIWindowScene else {return 0};
  41. guard let window = windowScene.windows.first else {return 0};
  42. return window.safeAreaInsets.bottom;
  43. } else if #available(iOS 11.0, *) {
  44. guard let window = UIApplication.shared.windows.first else {return 0};
  45. return window.safeAreaInsets.bottom;
  46. }
  47. return 0;
  48. }
  49. /** 屏幕宽度 */
  50. public let k_ScreenWidth = UIScreen.main.bounds.size.width
  51. /** 屏幕高度 */
  52. public let k_ScreenHeight = UIScreen.main.bounds.size.height
  53. /* 导航栏高度 固定高度 = 44.0f */
  54. //let k_Height_NavContentBar :CGFloat = UINavigationBar.appearance().frame.size.height
  55. public let k_Height_NavBar :CGFloat = 44.0
  56. /** 状态栏高度 */
  57. public let k_Height_StatusBar :CGFloat = k_Height_statusBar()
  58. /** 状态栏+导航栏的高度 */
  59. public let k_Nav_Height: CGFloat = k_Height_NavBar + k_Height_StatusBar
  60. /** 底部tabBar栏高度(不包含安全区,即:在 iphoneX 之前的手机) */
  61. public let k_TabBar_Height :CGFloat = 49.0
  62. /** 底部导航栏高度(包括安全区),一般使用这个值 */
  63. public let k_Height_TabBar :CGFloat = k_Height_safeAreaInsetsBottom() + k_TabBar_Height
  64. // MARK: - 检查系统版本 -
  65. /* 检查系统版本 */
  66. /// 版本号相同:
  67. public func systemVersionEqual(version:String) -> Bool {
  68. return UIDevice.current.systemVersion == version
  69. }
  70. /// 系统版本高于等于该version 测试发现只能传入带一位小数点的版本号 不然会报错 具体原因待探究
  71. public func systemVersionGreaterThan(version:String) -> Bool {
  72. return UIDevice.current.systemVersion.compare(version, options: .numeric, range: version.startIndex..<version.endIndex, locale: Locale(identifier:version)) != ComparisonResult.orderedAscending
  73. }
  74. //判断是否是 x、及x以上 系列
  75. public func isIphoneX() -> Bool {
  76. return k_Height_safeAreaInsetsBottom() > 0.0; // 底部安全区 > 0 时,
  77. }
  78. //设计稿为 375*812 比例,所以比例系数为 375.0/kScreenWidth
  79. public let kDesignScale = k_ScreenWidth/375.0
  80. public func kGetUIH(designSize:CGSize,currentW:CGFloat) -> CGFloat {
  81. let scale = designSize.width/currentW
  82. return designSize.height/scale
  83. }
  84. public func debugPrint<T>(_ messsage: T, file: String = #file, funcName: String = #function, lineNum: Int = #line) {
  85. #if DEBUG
  86. let fileName = (file as NSString).lastPathComponent
  87. print(Date.hmsString + " \(fileName) (\(funcName)): [\(lineNum)]- \(messsage)")
  88. #endif
  89. }
  90. public func dePrint<T>(_ messsage: T) {
  91. #if DEBUG
  92. print(Date.hmsString + " \(messsage)")
  93. #endif
  94. }
  95. public func className(from object: Any) -> String {
  96. return String(describing: type(of: object))
  97. }
  98. public func appVersion() ->String{
  99. let short = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
  100. return "V" + short
  101. }
  102. /// 震动
  103. public func playVibration() {
  104. // 默认震动 kSystemSoundID_Vibrate
  105. // 1519 短震 3D Touch中的peek震动反馈
  106. // 1520 短震 3D Touch中的pop震动反馈
  107. // 1521 连续三次短震动
  108. // peek的震动反馈轻于pop
  109. let soundID: UInt32 = 1520
  110. AudioServicesPlaySystemSound(soundID)
  111. }
  112. public func kPresentModalVC(target:UIViewController,
  113. modelVC:UIViewController,
  114. style:UIModalPresentationStyle = .overFullScreen,
  115. transitionStyle:UIModalTransitionStyle = .coverVertical,
  116. completion: (() -> Void)? = nil){
  117. let navi = TSBaseNavigationC(rootViewController: modelVC)
  118. navi.modalPresentationStyle = style
  119. navi.modalTransitionStyle = transitionStyle
  120. target.present(navi, animated: true,completion: completion)
  121. }
  122. public func kPushVC(target:UIViewController,modelVC:UIViewController){
  123. modelVC.hidesBottomBarWhenPushed = true
  124. target.navigationController?.pushViewController(modelVC, animated: true)
  125. }