BaseTabbarViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // BaseTabbarViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/3/8.
  6. // Copyright © 2019 南鑫林. All rights reserved.
  7. //
  8. import UIKit
  9. import Lottie
  10. import ESTabBarController_swift
  11. import PPBadgeViewSwift
  12. class BaseTabbarViewController: NSObject {
  13. static let shared : BaseTabbarViewController = BaseTabbarViewController()
  14. var esTabBarController : ESTabBarController!
  15. /// 社区
  16. var v1 : CommunityViewController!
  17. /// 商城
  18. // var v2 = ShoppingMallViewController!
  19. /// 兑换专区
  20. var v2 : BackyardViewController!
  21. /// 发布
  22. var v3 : BaseViewController!
  23. /// 消息
  24. var v4 : MessageMainViewController!
  25. /// 我的
  26. var v5 : UserPersonalCenterViewController!
  27. /// 社区
  28. var lottieAnimateContentView1 : BaseLottieAnimateContentView!
  29. /// 商城
  30. var lottieAnimateContentView2 : BaseLottieAnimateContentView!
  31. /// 消息
  32. var lottieAnimateContentView4 : BaseLottieAnimateContentView!
  33. /// 我的
  34. var lottieAnimateContentView5 : BaseLottieAnimateContentView!
  35. var lastIndex : Int = 0
  36. func tabBarViewController() -> ESTabBarController {
  37. esTabBarController = ESTabBarController()
  38. // if #available(iOS 13.0, *) {
  39. // esTabBarController.tabBar.standardAppearance.configureWithOpaqueBackground()
  40. // esTabBarController.tabBar.standardAppearance.backgroundImage = UIImage.imageWithColor(color: UIColor.white)
  41. // esTabBarController.tabBar.standardAppearance.shadowImage = UIImage(named: "navbar_shadow_pic_down")
  42. // esTabBarController.tabBar.standardAppearance.shadowColor = UIColor.yellow
  43. // } else {
  44. esTabBarController.tabBar.shadowImage = UIImage(named: "navbar_shadow_pic_down")
  45. // }
  46. esTabBarController.shouldHijackHandler = {
  47. [weak self] (tabbarController, viewController, index) in
  48. AudioServicesPlaySystemSound(1519)
  49. if UserModel.isTokenNil() {
  50. if index == 2 || index == 3 || index == 4 {
  51. return true
  52. }else {
  53. self?.reloadTabbar(index:index)
  54. self?.lastIndex = index
  55. return false
  56. }
  57. }else {
  58. if index == 2 {
  59. return true
  60. }else {
  61. self?.reloadTabbar(index:index)
  62. self?.lastIndex = index
  63. return false
  64. }
  65. }
  66. }
  67. esTabBarController.didHijackHandler = {
  68. [weak self] tabbarController, viewController, index in
  69. if UserModel.isTokenNil() {
  70. kAppDelegate.setLogin()
  71. }else {
  72. self?.pushPublishVC()
  73. }
  74. }
  75. /// 社区
  76. v1 = CommunityViewController()
  77. /// 商城
  78. // var v2 = ShoppingMallViewController!
  79. /// 兑换专区
  80. v2 = BackyardViewController()
  81. /// 发布
  82. v3 = BaseViewController()
  83. /// 消息
  84. v4 = MessageMainViewController()
  85. /// 我的
  86. v5 = UserPersonalCenterViewController()
  87. /// 社区
  88. lottieAnimateContentView1 = BaseLottieAnimateContentView(jsonStr: "communityModuleTabbar")
  89. /// 商城
  90. lottieAnimateContentView2 = BaseLottieAnimateContentView(jsonStr: "shoppingMallModuleTabbar")
  91. /// 消息
  92. lottieAnimateContentView4 = BaseLottieAnimateContentView(jsonStr: "messageModuleTabbar")
  93. /// 我的
  94. lottieAnimateContentView5 = BaseLottieAnimateContentView(jsonStr: "MineModuleTabbar")
  95. // v1.tabBarItem = ESTabBarItem.init(lottieAnimateContentView1, title: "社区", image: UIImage(named: "tabbar_home")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
  96. // v2.tabBarItem = ESTabBarItem.init(lottieAnimateContentView2, title: "福利", image: UIImage(named: "tabbar_shopping")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
  97. tabBarItemStyle(navc: v1, normalImg: "tabbar_home", selectorImg: "tabbar_home_pre", title: "社区")
  98. tabBarItemStyle(navc: v2, normalImg: "tabbar_shopping", selectorImg: "tabbar_shopping_pre", title: "后院")
  99. v3.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
  100. v3.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0);
  101. tabBarItemStyle(navc: v4, normalImg: "tabbar_massage", selectorImg: "tabbar_massage_pre", title: "消息")
  102. tabBarItemStyle(navc: v5, normalImg: "tabbar_my", selectorImg: "tabbar_my_pre", title: "我的")
  103. // if UserModel.isTokenNil() {
  104. // setLoginOutTabBarItem()
  105. // }else {
  106. // setLoginTabBarItem()
  107. // }
  108. let n1 = BaseNavigationViewController.init(rootViewController: v1)
  109. let n2 = BaseNavigationViewController.init(rootViewController: v2)
  110. let n3 = BaseNavigationViewController.init(rootViewController: v3)
  111. let n4 = BaseNavigationViewController.init(rootViewController: v4)
  112. let n5 = BaseNavigationViewController.init(rootViewController: v5)
  113. esTabBarController.viewControllers = [n1, n2, n3, n4, n5]
  114. //将要进入前台的时候
  115. setBadge()
  116. NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
  117. [weak self] (notification) in
  118. if !UserModel.isTokenNil() {
  119. //获取消息的小红点
  120. MessageModuleManager.shared.userMemberMessageIndexApi()
  121. self?.setBadge()
  122. }
  123. })
  124. //监听消息
  125. NotificationCenter.default.addObserver(forName: NSNotification.Name("MessageMainViewController"), object: nil, queue: OperationQueue.main) {
  126. [weak self] notification in
  127. self?.setBadge()
  128. }
  129. return esTabBarController
  130. }
  131. func setLoginTabBarItem() {
  132. // v4.tabBarItem = ESTabBarItem.init(lottieAnimateContentView4, title: "消息", image: UIImage(named: "tabbar_massage")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
  133. // v5.tabBarItem = ESTabBarItem.init(lottieAnimateContentView5, title: "我的", image: UIImage(named: "tabbar_my")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
  134. tabBarItemStyle(navc: v1, normalImg: "tabbar_home", selectorImg: "tabbar_home_pre", title: "社区")
  135. tabBarItemStyle(navc: v2, normalImg: "tabbar_shopping", selectorImg: "tabbar_shopping_pre", title: "后院")
  136. v3.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
  137. v3.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0);
  138. tabBarItemStyle(navc: v4, normalImg: "tabbar_massage", selectorImg: "tabbar_massage_pre", title: "消息")
  139. tabBarItemStyle(navc: v5, normalImg: "tabbar_my", selectorImg: "tabbar_my_pre", title: "我的")
  140. }
  141. func setLoginOutTabBarItem() {
  142. // v4.tabBarItem = ESTabBarItem.init(BaseTabBarItemContentView(), title: "消息", image: UIImage(named: "tabbar_massage")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage:nil)
  143. // v5.tabBarItem = ESTabBarItem.init(BaseTabBarItemContentView(), title: "我的", image: UIImage(named: "tabbar_my")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage:nil)
  144. tabBarItemStyle(navc: v1, normalImg: "tabbar_home", selectorImg: "tabbar_home_pre", title: "社区")
  145. tabBarItemStyle(navc: v2, normalImg: "tabbar_shopping", selectorImg: "tabbar_shopping_pre", title: "后院")
  146. v3.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
  147. v3.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0);
  148. tabBarItemStyle(navc: v4, normalImg: "tabbar_massage", selectorImg: "tabbar_massage_pre", title: "消息")
  149. tabBarItemStyle(navc: v5, normalImg: "tabbar_my", selectorImg: "tabbar_my_pre", title: "我的")
  150. }
  151. /// 跳转到VC
  152. func pushPublishVC() {
  153. if BaseTabbarViewController.shared.v1.navigationBarbgView.isHidden {
  154. let pickVc = PublishViewController()
  155. // let pickVc = PublishNewViewController()
  156. let nav = BaseNavigationViewController.init(rootViewController: pickVc)
  157. nav.modalPresentationStyle = .fullScreen
  158. BaseTabbarViewController.shared.esTabBarController.present(nav, animated: true, completion: nil)
  159. } else {
  160. SwiftProgressHUD.shared().showText("内容上传中,请稍等", textAlignment: .center)
  161. }
  162. }
  163. // 设置Badge
  164. func setBadge() {
  165. if UserModel.isTokenNil() {
  166. v4.tabBarItem.pp.hiddenBadge()
  167. v5.tabBarItem.pp.hiddenBadge()
  168. }else {
  169. //消息
  170. let messageIndexModel = MessageIndexModel.shared.object()
  171. if messageIndexModel?.activity?.isYes == 1 || messageIndexModel?.follow?.isYes == 1 || messageIndexModel?.notification?.isYes == 1 || messageIndexModel?.praise?.isYes == 1 || messageIndexModel?.comment?.isYes == 1 {
  172. v4.tabBarItem.pp.addDot(color:kFE352BColor)
  173. v4.tabBarItem.pp.moveBadge(x: -3,y: 3)
  174. // let tabBarItem = v4.tabBarItem as? ESTabBarItem
  175. // tabBarItem?.contentView?.badgeColor = kFE352BColor
  176. // tabBarItem?.contentView?.badgeValue = ""
  177. // tabBarItem?.contentView?.badgeOffset = UIOffset(horizontal: 0, vertical: -22)
  178. }else {
  179. // let tabBarItem = v4.tabBarItem as? ESTabBarItem
  180. // tabBarItem?.contentView?.badgeValue = nil
  181. v4.tabBarItem.pp.hiddenBadge()
  182. }
  183. //我的
  184. if UpdateVersionModel.shared.object()?.upGradeModelType == .update || UpdateVersionModel.shared.object()?.upGradeModelType == .strongUpdate {
  185. // let tabBarItem = v5.tabBarItem as? ESTabBarItem
  186. // tabBarItem?.contentView?.badgeColor = kFE352BColor
  187. // tabBarItem?.contentView?.badgeValue = ""
  188. // tabBarItem?.contentView?.badgeOffset = UIOffset(horizontal: 0, vertical: -22)
  189. v5.tabBarItem.pp.addDot(color:kFE352BColor)
  190. v5.tabBarItem.pp.moveBadge(x: -3,y: 3)
  191. }else {
  192. // let tabBarItem = v5.tabBarItem as? ESTabBarItem
  193. // tabBarItem?.contentView?.badgeValue = nil
  194. v5.tabBarItem.pp.hiddenBadge()
  195. }
  196. }
  197. }
  198. func reloadTabbar(index:Int) {
  199. if index == 0 && lastIndex == 0 {
  200. // self.lottieAnimateContentView1.animationView.stop()
  201. // self.lottieAnimateContentView1.animationView.play()
  202. if communityModuleTopType == .communityModuleFollowTop {
  203. NotificationCenter.default.post(name: NSNotification.Name("CommunityModuleFollowTop"), object: nil)
  204. }
  205. if communityModuleTopType == .communityModuleRecommendTop {
  206. NotificationCenter.default.post(name: NSNotification.Name("CommunityModuleRecommendTop"), object: nil)
  207. }
  208. }
  209. if index == 1 && lastIndex == 1 {
  210. // self.lottieAnimateContentView2.animationView.stop()
  211. // self.lottieAnimateContentView2.animationView.play()
  212. NotificationCenter.default.post(name: NSNotification.Name("BackyardViewModuleTop"), object: nil)
  213. }
  214. if index == 3 && lastIndex == 3 {
  215. // self.lottieAnimateContentView4.animationView.stop()
  216. // self.lottieAnimateContentView4.animationView.play()
  217. NotificationCenter.default.post(name: NSNotification.Name("MessageModuleTop"), object: nil)
  218. }
  219. if index == 4 && lastIndex == 4 {
  220. // self.lottieAnimateContentView5.animationView.stop()
  221. // self.lottieAnimateContentView5.animationView.play()
  222. NotificationCenter.default.post(name: NSNotification.Name("MineModuleTop"), object: nil)
  223. }
  224. }
  225. private func tabBarItemStyle(navc : BaseViewController, normalImg : String, selectorImg : String, title : String){
  226. navc.tabBarItem = UITabBarItem.init(title: title, image: UIImage(named:normalImg), selectedImage: UIImage(named:selectorImg))
  227. navc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:k333333Color], for: UIControl.State.normal)
  228. navc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:k333333Color], for: UIControl.State.selected)
  229. }
  230. }
  231. extension ESTabBar {
  232. open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
  233. let view = super.hitTest(point, with: event)
  234. if view == nil {
  235. if UserModel.isTokenNil() && LoginNowView.shared.loginNowView != nil {
  236. for subView in subviews {
  237. if !subView.subviews.isEmpty {
  238. for subView in subView.subviews {
  239. let myPoint = subView.convert(point, from: self)
  240. if subView.bounds.contains(myPoint) {
  241. return subView
  242. }
  243. }
  244. }
  245. let myPoint = subView.convert(point, from: self)
  246. if subView.bounds.contains(myPoint) {
  247. return subView
  248. }
  249. }
  250. }
  251. }
  252. return view
  253. }
  254. }