123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- //
- // BaseTabbarViewController.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/3/8.
- // Copyright © 2019 南鑫林. All rights reserved.
- //
- import UIKit
- import Lottie
- import ESTabBarController_swift
- import PPBadgeViewSwift
- class BaseTabbarViewController: NSObject {
- static let shared : BaseTabbarViewController = BaseTabbarViewController()
-
- var esTabBarController : ESTabBarController!
-
- /// 社区
- var v1 : CommunityViewController!
- /// 商城
- // var v2 = ShoppingMallViewController!
- /// 兑换专区
- var v2 : BackyardViewController!
- /// 发布
- var v3 : BaseViewController!
- /// 消息
- var v4 : MessageMainViewController!
- /// 我的
- var v5 : UserPersonalCenterViewController!
-
- /// 社区
- var lottieAnimateContentView1 : BaseLottieAnimateContentView!
-
- /// 商城
- var lottieAnimateContentView2 : BaseLottieAnimateContentView!
-
- /// 消息
- var lottieAnimateContentView4 : BaseLottieAnimateContentView!
-
- /// 我的
- var lottieAnimateContentView5 : BaseLottieAnimateContentView!
-
- var lastIndex : Int = 0
- func tabBarViewController() -> ESTabBarController {
- esTabBarController = ESTabBarController()
-
- // if #available(iOS 13.0, *) {
- // esTabBarController.tabBar.standardAppearance.configureWithOpaqueBackground()
- // esTabBarController.tabBar.standardAppearance.backgroundImage = UIImage.imageWithColor(color: UIColor.white)
- // esTabBarController.tabBar.standardAppearance.shadowImage = UIImage(named: "navbar_shadow_pic_down")
- // esTabBarController.tabBar.standardAppearance.shadowColor = UIColor.yellow
- // } else {
- esTabBarController.tabBar.shadowImage = UIImage(named: "navbar_shadow_pic_down")
- // }
-
- esTabBarController.shouldHijackHandler = {
- [weak self] (tabbarController, viewController, index) in
- AudioServicesPlaySystemSound(1519)
- if UserModel.isTokenNil() {
- if index == 2 || index == 3 || index == 4 {
- return true
- }else {
- self?.reloadTabbar(index:index)
- self?.lastIndex = index
- return false
- }
- }else {
- if index == 2 {
- return true
- }else {
- self?.reloadTabbar(index:index)
- self?.lastIndex = index
- return false
- }
- }
-
-
- }
- esTabBarController.didHijackHandler = {
- [weak self] tabbarController, viewController, index in
- if UserModel.isTokenNil() {
- kAppDelegate.setLogin()
- }else {
- self?.pushPublishVC()
- }
- }
-
- /// 社区
- v1 = CommunityViewController()
- /// 商城
- // var v2 = ShoppingMallViewController!
- /// 兑换专区
- v2 = BackyardViewController()
- /// 发布
- v3 = BaseViewController()
- /// 消息
- v4 = MessageMainViewController()
- /// 我的
- v5 = UserPersonalCenterViewController()
-
- /// 社区
- lottieAnimateContentView1 = BaseLottieAnimateContentView(jsonStr: "communityModuleTabbar")
-
- /// 商城
- lottieAnimateContentView2 = BaseLottieAnimateContentView(jsonStr: "shoppingMallModuleTabbar")
-
- /// 消息
- lottieAnimateContentView4 = BaseLottieAnimateContentView(jsonStr: "messageModuleTabbar")
-
- /// 我的
- lottieAnimateContentView5 = BaseLottieAnimateContentView(jsonStr: "MineModuleTabbar")
-
- // v1.tabBarItem = ESTabBarItem.init(lottieAnimateContentView1, title: "社区", image: UIImage(named: "tabbar_home")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
- // v2.tabBarItem = ESTabBarItem.init(lottieAnimateContentView2, title: "福利", image: UIImage(named: "tabbar_shopping")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
-
- tabBarItemStyle(navc: v1, normalImg: "tabbar_home", selectorImg: "tabbar_home_pre", title: "社区")
- tabBarItemStyle(navc: v2, normalImg: "tabbar_shopping", selectorImg: "tabbar_shopping_pre", title: "后院")
- v3.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
- v3.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0);
- tabBarItemStyle(navc: v4, normalImg: "tabbar_massage", selectorImg: "tabbar_massage_pre", title: "消息")
- tabBarItemStyle(navc: v5, normalImg: "tabbar_my", selectorImg: "tabbar_my_pre", title: "我的")
-
- // if UserModel.isTokenNil() {
- // setLoginOutTabBarItem()
- // }else {
- // setLoginTabBarItem()
- // }
- let n1 = BaseNavigationViewController.init(rootViewController: v1)
- let n2 = BaseNavigationViewController.init(rootViewController: v2)
- let n3 = BaseNavigationViewController.init(rootViewController: v3)
- let n4 = BaseNavigationViewController.init(rootViewController: v4)
- let n5 = BaseNavigationViewController.init(rootViewController: v5)
- esTabBarController.viewControllers = [n1, n2, n3, n4, n5]
- //将要进入前台的时候
- setBadge()
- NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: OperationQueue.main, using: {
- [weak self] (notification) in
- if !UserModel.isTokenNil() {
- //获取消息的小红点
- MessageModuleManager.shared.userMemberMessageIndexApi()
- self?.setBadge()
- }
- })
- //监听消息
- NotificationCenter.default.addObserver(forName: NSNotification.Name("MessageMainViewController"), object: nil, queue: OperationQueue.main) {
- [weak self] notification in
- self?.setBadge()
- }
- return esTabBarController
- }
-
- func setLoginTabBarItem() {
- // v4.tabBarItem = ESTabBarItem.init(lottieAnimateContentView4, title: "消息", image: UIImage(named: "tabbar_massage")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
- // v5.tabBarItem = ESTabBarItem.init(lottieAnimateContentView5, title: "我的", image: UIImage(named: "tabbar_my")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: nil)
- tabBarItemStyle(navc: v1, normalImg: "tabbar_home", selectorImg: "tabbar_home_pre", title: "社区")
- tabBarItemStyle(navc: v2, normalImg: "tabbar_shopping", selectorImg: "tabbar_shopping_pre", title: "后院")
- v3.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
- v3.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0);
- tabBarItemStyle(navc: v4, normalImg: "tabbar_massage", selectorImg: "tabbar_massage_pre", title: "消息")
- tabBarItemStyle(navc: v5, normalImg: "tabbar_my", selectorImg: "tabbar_my_pre", title: "我的")
- }
-
- func setLoginOutTabBarItem() {
- // v4.tabBarItem = ESTabBarItem.init(BaseTabBarItemContentView(), title: "消息", image: UIImage(named: "tabbar_massage")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage:nil)
- // v5.tabBarItem = ESTabBarItem.init(BaseTabBarItemContentView(), title: "我的", image: UIImage(named: "tabbar_my")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage:nil)
- tabBarItemStyle(navc: v1, normalImg: "tabbar_home", selectorImg: "tabbar_home_pre", title: "社区")
- tabBarItemStyle(navc: v2, normalImg: "tabbar_shopping", selectorImg: "tabbar_shopping_pre", title: "后院")
- v3.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal), selectedImage: UIImage(named: "tabbar_publish")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal))
- v3.tabBarItem.imageInsets = UIEdgeInsets(top: 4, left: 0, bottom: -4, right: 0);
- tabBarItemStyle(navc: v4, normalImg: "tabbar_massage", selectorImg: "tabbar_massage_pre", title: "消息")
- tabBarItemStyle(navc: v5, normalImg: "tabbar_my", selectorImg: "tabbar_my_pre", title: "我的")
- }
-
- /// 跳转到VC
- func pushPublishVC() {
- if BaseTabbarViewController.shared.v1.navigationBarbgView.isHidden {
- let pickVc = PublishViewController()
- // let pickVc = PublishNewViewController()
- let nav = BaseNavigationViewController.init(rootViewController: pickVc)
- nav.modalPresentationStyle = .fullScreen
- BaseTabbarViewController.shared.esTabBarController.present(nav, animated: true, completion: nil)
- } else {
- SwiftProgressHUD.shared().showText("内容上传中,请稍等", textAlignment: .center)
- }
- }
-
- // 设置Badge
- func setBadge() {
- if UserModel.isTokenNil() {
- v4.tabBarItem.pp.hiddenBadge()
- v5.tabBarItem.pp.hiddenBadge()
- }else {
- //消息
- let messageIndexModel = MessageIndexModel.shared.object()
- if messageIndexModel?.activity?.isYes == 1 || messageIndexModel?.follow?.isYes == 1 || messageIndexModel?.notification?.isYes == 1 || messageIndexModel?.praise?.isYes == 1 || messageIndexModel?.comment?.isYes == 1 {
- v4.tabBarItem.pp.addDot(color:kFE352BColor)
- v4.tabBarItem.pp.moveBadge(x: -3,y: 3)
-
- // let tabBarItem = v4.tabBarItem as? ESTabBarItem
- // tabBarItem?.contentView?.badgeColor = kFE352BColor
- // tabBarItem?.contentView?.badgeValue = ""
- // tabBarItem?.contentView?.badgeOffset = UIOffset(horizontal: 0, vertical: -22)
- }else {
- // let tabBarItem = v4.tabBarItem as? ESTabBarItem
- // tabBarItem?.contentView?.badgeValue = nil
- v4.tabBarItem.pp.hiddenBadge()
- }
-
- //我的
- if UpdateVersionModel.shared.object()?.upGradeModelType == .update || UpdateVersionModel.shared.object()?.upGradeModelType == .strongUpdate {
-
- // let tabBarItem = v5.tabBarItem as? ESTabBarItem
- // tabBarItem?.contentView?.badgeColor = kFE352BColor
- // tabBarItem?.contentView?.badgeValue = ""
- // tabBarItem?.contentView?.badgeOffset = UIOffset(horizontal: 0, vertical: -22)
- v5.tabBarItem.pp.addDot(color:kFE352BColor)
- v5.tabBarItem.pp.moveBadge(x: -3,y: 3)
- }else {
- // let tabBarItem = v5.tabBarItem as? ESTabBarItem
- // tabBarItem?.contentView?.badgeValue = nil
- v5.tabBarItem.pp.hiddenBadge()
- }
- }
- }
-
- func reloadTabbar(index:Int) {
-
- if index == 0 && lastIndex == 0 {
- // self.lottieAnimateContentView1.animationView.stop()
- // self.lottieAnimateContentView1.animationView.play()
- if communityModuleTopType == .communityModuleFollowTop {
- NotificationCenter.default.post(name: NSNotification.Name("CommunityModuleFollowTop"), object: nil)
-
- }
- if communityModuleTopType == .communityModuleRecommendTop {
- NotificationCenter.default.post(name: NSNotification.Name("CommunityModuleRecommendTop"), object: nil)
-
- }
-
- }
- if index == 1 && lastIndex == 1 {
- // self.lottieAnimateContentView2.animationView.stop()
- // self.lottieAnimateContentView2.animationView.play()
- NotificationCenter.default.post(name: NSNotification.Name("BackyardViewModuleTop"), object: nil)
-
- }
- if index == 3 && lastIndex == 3 {
- // self.lottieAnimateContentView4.animationView.stop()
- // self.lottieAnimateContentView4.animationView.play()
- NotificationCenter.default.post(name: NSNotification.Name("MessageModuleTop"), object: nil)
-
- }
- if index == 4 && lastIndex == 4 {
- // self.lottieAnimateContentView5.animationView.stop()
- // self.lottieAnimateContentView5.animationView.play()
- NotificationCenter.default.post(name: NSNotification.Name("MineModuleTop"), object: nil)
- }
- }
- private func tabBarItemStyle(navc : BaseViewController, normalImg : String, selectorImg : String, title : String){
- navc.tabBarItem = UITabBarItem.init(title: title, image: UIImage(named:normalImg), selectedImage: UIImage(named:selectorImg))
- navc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:k333333Color], for: UIControl.State.normal)
- navc.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:k333333Color], for: UIControl.State.selected)
- }
-
- }
- extension ESTabBar {
- open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
-
- let view = super.hitTest(point, with: event)
- if view == nil {
- if UserModel.isTokenNil() && LoginNowView.shared.loginNowView != nil {
- for subView in subviews {
- if !subView.subviews.isEmpty {
- for subView in subView.subviews {
- let myPoint = subView.convert(point, from: self)
- if subView.bounds.contains(myPoint) {
- return subView
- }
- }
- }
- let myPoint = subView.convert(point, from: self)
- if subView.bounds.contains(myPoint) {
- return subView
- }
- }
- }
- }
- return view
- }
- }
|