CustomTabBarController.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // CustomTabBarController.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/10.
  6. //
  7. import UIKit
  8. class CustomTabBarController: UITabBarController {
  9. lazy var blurTabBar: UIView = {
  10. let _blurTabBar = UIView(frame: CGRect(x: 0, y: -1, width: QSLConst.qsl_kScreenW, height: QSLConst.qsl_kScreenH))
  11. _blurTabBar.isUserInteractionEnabled = true
  12. self.tabBar.addSubview(_blurTabBar)
  13. return _blurTabBar
  14. }()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. if(QSLCacheManager.getModel() == nil){
  18. requestConfig()
  19. Thread.sleep(forTimeInterval: 1)
  20. }else{
  21. requestConfig()
  22. }
  23. // 设置 tabbar 样式
  24. self.setTabBarAppearence()
  25. // 添加 tab 页
  26. self.addFoundationTab()
  27. // 默认进入页面到主页
  28. self.selectedIndex = 1
  29. managerAppOnlyLaunch()
  30. }
  31. func requestConfig(){
  32. QSLNetwork().request(.confGlobal(dict: [String: Any]())) { response in
  33. var model = response.mapObject(QSLConfModel.self, modelKey: "data")
  34. QSLCacheManager.cacheConfModel(model)
  35. } fail: { code, msg in
  36. if(QSLCacheManager.getModel() == nil){
  37. let model = QSLConfModel.init()
  38. QSLCacheManager.cacheConfModel(model)
  39. }
  40. }
  41. }
  42. //管理app启动埋点
  43. func managerAppOnlyLaunch() {
  44. guard UserDefaults.standard.string(forKey: "MANAGER_APP_ONLY_LAUNCH") != nil else {
  45. UserDefaults.standard.set("YES", forKey: "MANAGER_APP_ONLY_LAUNCH")
  46. UserDefaults.standard.synchronize()
  47. //只有app第一次启动才会埋点
  48. QSEventHandle.gravityPush(eventName: QSLGravityConst.app_launch, eventProps: ["id": 02001])
  49. return
  50. }
  51. }
  52. func addFoundationTab() {
  53. self.addFriendTab()
  54. self.addHomeTab()
  55. self.addCheckTab()
  56. self.addMineTab()
  57. }
  58. }
  59. extension CustomTabBarController {
  60. func setTabBarAppearence() {
  61. self.tabBar.backgroundColor = .white
  62. self.tabBar.shadowImage = UIImage.image(color: .hexStringColor(hexString: "#EEEEEE"), size: CGSize(width: QSLConst.qsl_kScreenW, height: 1))
  63. }
  64. }
  65. extension CustomTabBarController: UITabBarControllerDelegate {
  66. override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
  67. switch item.tag {
  68. case 0:
  69. QSEventHandle.eventPush(eventName: QSLGravityConst.tab_friend)
  70. break
  71. case 1:
  72. QSEventHandle.eventPush(eventName: QSLGravityConst.tab_location)
  73. break
  74. case 2:
  75. QSEventHandle.eventPush(eventName: QSLGravityConst.tab_message)
  76. break
  77. case 3:
  78. QSEventHandle.eventPush(eventName: QSLGravityConst.tab_mine)
  79. break
  80. default:
  81. break
  82. }
  83. }
  84. }
  85. extension CustomTabBarController {
  86. /// 添加 好友 Tab
  87. func addFriendTab() {
  88. let imageInsets = UIEdgeInsets.zero
  89. let titlePostion = UIOffset(horizontal: 0, vertical: 0)
  90. let vc = QSLFriendController()
  91. self.addChildViewController(childVc: vc, title: "好友", image: "tab_friends_normal", selectedImage: "tab_friends_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 0)
  92. }
  93. /// 添加 主页定位 Tab
  94. func addHomeTab() {
  95. let imageInsets = UIEdgeInsets.zero
  96. let titlePostion = UIOffset(horizontal: 0, vertical: 0)
  97. let vc = QSLHomeController()
  98. self.addChildViewController(childVc: vc, title: "定位", image: "tab_location_normal", selectedImage: "tab_location_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 1)
  99. }
  100. /// 添加 消息 Tab
  101. func addCheckTab() {
  102. let imageInsets = UIEdgeInsets.zero
  103. let titlePostion = UIOffset(horizontal: 0, vertical: 0)
  104. let vc = QSLMessageController()
  105. self.addChildViewController(childVc: vc, title: "消息", image: "tab_message_normal", selectedImage: "tab_message_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 2)
  106. }
  107. /// 添加我的 Tab
  108. func addMineTab() {
  109. let imageInsets = UIEdgeInsets.zero
  110. let titlePostion = UIOffset(horizontal: 0, vertical: 0)
  111. let vc = QSLMineController()
  112. self.addChildViewController(childVc: vc, title: "我的", image: "tab_mine_normal", selectedImage: "tab_mine_selected", imageInsets: imageInsets, titlePosition: titlePostion, index: 3)
  113. }
  114. }