YOUPAILCTabBarVC.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. //
  2. // YOUPAILCTabBarVC.m
  3. // LiveChat
  4. //
  5. // Created by 张灿 on 2018/4/6.
  6. // Copyright © 2018年 DaTongIntelligent. All rights reserved.
  7. //
  8. #import "YOUPAILCTabBarVC.h"
  9. #import "YMHomePageViewController.h"
  10. #import "YMHomeController.h"
  11. #import "YMDynamicViewController.h"
  12. #import "YMRankingViewController.h"
  13. #import "YMMineViewController.h"
  14. #import "YOUPAILCMessageVC.h"
  15. #import "YOUPAILCVerifyBigCastVC.h"
  16. #import "YOUPAILCBoardVC.h"
  17. #import "YOUPAILCMsgVC.h"
  18. #import "YOUPAILZLiveVC.h"
  19. #import "YOUPAIHRChatRoomHomeVC.h"
  20. #import "AppDelegate.h"
  21. #import "YOUPAILZProfileVC.h"
  22. #import "YOUPAILZDiscoverVC.h"
  23. #import "YOUPAIBBProfileVC.h"
  24. #import "YOUPAIBBMsgVC.h"
  25. #import "YOUPAILZHomeVC.h"
  26. @interface YOUPAILCTabBarVC ()<NIMConversationManagerDelegate,UITabBarDelegate,UITabBarControllerDelegate>
  27. @property(nonatomic,assign)NSInteger youpaipindexFlag;
  28. @property(nonatomic,assign)NSInteger youpaipcurrentIndex;
  29. @end
  30. @implementation YOUPAILCTabBarVC
  31. ///<NIMConversationManagerDelegate>
  32. + (instancetype)instance{
  33. AppDelegate *delegete = (AppDelegate *)[UIApplication sharedApplication].delegate;
  34. UIViewController *vc = delegete.window.rootViewController;
  35. if ([vc isKindOfClass:[YOUPAILCTabBarVC class]]) {
  36. return (YOUPAILCTabBarVC *)vc;
  37. }else{
  38. return nil;
  39. }
  40. }
  41. - (void)viewWillAppear:(BOOL)animated{
  42. [super viewWillAppear:animated];
  43. [[NIMSDK sharedSDK].conversationManager addDelegate:self];
  44. [self youpaifupdateUnread];
  45. }
  46. #pragma mark 修复严重内存泄漏1
  47. //add by leo delegate 要移除
  48. -(void)viewWillDisappear:(BOOL)animated
  49. {
  50. NSLog(@"tabbar viewWillDisappear");
  51. [super viewWillDisappear:YES];
  52. //modify by leo 不移除 又内存泄漏
  53. [[NIMSDK sharedSDK].conversationManager removeDelegate:self];
  54. }
  55. - (void)viewDidLoad {
  56. [super viewDidLoad];
  57. self.delegate = self;
  58. [self youpaifsetUpAllChildVc];
  59. // self.delegate = self;
  60. //创建自己的tabbar,然后用KVC将自己的tabbar和系统的tabBar替换下
  61. YOUPAILCTabBar *tabbar = [[YOUPAILCTabBar alloc] init];
  62. tabbar.itemPositioning = UITabBarItemPositioningFill;
  63. tabbar.tintColor = DecColorFromRGBA(0, 0, 0, 1);//HexColorFromRGB(0xA7A9FD);
  64. // [self.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:10]} forState: UIControlStateNormal];
  65. // [self.tabBarItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:12]} forState: UIControlStateSelected];
  66. // tabbar.barTintColor = LCTextNormal;
  67. if (@available(iOS 10.0, *)) {
  68. tabbar.unselectedItemTintColor = DecColorFromRGBA(0, 0, 0, 0.6);
  69. } else {
  70. // Fallback on earlier versions
  71. }
  72. tabbar.barTintColor = [UIColor whiteColor];
  73. tabbar.backgroundColor = [UIColor whiteColor];
  74. // tabbar.block = ^{
  75. // if ([LCSaveModel getUserModel].userinfo.is_anchor ==1) {
  76. // LCTabCenterView* centerView = [[LCTabCenterView alloc]init];
  77. // [self.view addSubview:centerView];
  78. // }else{
  79. // ZCBaseNavigationVC* nav = (ZCBaseNavigationVC*)self.selectedViewController;
  80. // YOUPAILCVerifyBigCastVC* verify = [[YOUPAILCVerifyBigCastVC alloc]init];
  81. // [nav.viewControllers[0].navigationController pushViewController:verify animated:YES];
  82. // }
  83. // };
  84. self.youpaiptabbar = tabbar;
  85. // self.tabBar.delegate = self;
  86. //KVC实质是修改了系统的_tabBar
  87. [self setValue:tabbar forKeyPath:@"tabBar"];
  88. self.selectedIndex = 0;
  89. self.youpaipindexFlag = 0;
  90. [[NIMSDK sharedSDK].conversationManager addDelegate:self];
  91. [self youpaifupdateUnread];
  92. // for (int i = 0; i< self.tabbar.subviews.count; i++) {
  93. // UIView *tabBarButton = self.tabBar.subviews[i];
  94. //
  95. // tabBarButton.tag = i;
  96. // if (i < 2) {
  97. // UITapGestureRecognizer *doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];
  98. // doubleRecognizer.numberOfTapsRequired = 2;// 双击
  99. // [tabBarButton addGestureRecognizer:doubleRecognizer];
  100. // }
  101. // }
  102. }
  103. //-(void)DoubleTap:(UITapGestureRecognizer *)tap
  104. //{
  105. // NSInteger index = tap.view.tag;
  106. // if (self.currentIndex == index && self.currentIndex == 0) {
  107. // //
  108. // }else {
  109. //
  110. // }
  111. //
  112. //}
  113. - (void)youpaifupdateUnread{
  114. if ([LCSaveData getWhiteVersion]) {
  115. NIMSession *session1 = [NIMSession session:@"2" type:NIMSessionTypeP2P];
  116. [[NIMSDK sharedSDK].conversationManager markAllMessagesReadInSession:session1];
  117. NIMSession *session2 = [NIMSession session:@"11" type:NIMSessionTypeP2P];
  118. [[NIMSDK sharedSDK].conversationManager markAllMessagesReadInSession:session2];
  119. }
  120. NSInteger unread = [[NIMSDK sharedSDK].conversationManager allUnreadCount];
  121. NSInteger item = [LCSaveData getWhiteVersion] ? 3 : 3;
  122. UITabBarItem* msgItm = self.youpaiptabbar.items[item];
  123. if (unread>0) {
  124. if (@available(iOS 10.0, *)) {
  125. msgItm.badgeColor = ZYGradientTwoColor;
  126. } else {
  127. // Fallback on earlier versions
  128. }
  129. if (unread>99) {
  130. msgItm.badgeValue = @"99+";
  131. }else{
  132. msgItm.badgeValue = [NSString stringWithFormat:@"%zd",unread];
  133. }
  134. }else{
  135. msgItm.badgeValue = nil;
  136. }
  137. }
  138. #pragma mark - 会话代理
  139. - (void)allMessagesRead{
  140. [self youpaifupdateUnread];
  141. }
  142. - (void)didAddRecentSession:(NIMRecentSession *)recentSession
  143. totalUnreadCount:(NSInteger)totalUnreadCount{
  144. [self youpaifupdateUnread];
  145. }
  146. - (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
  147. totalUnreadCount:(NSInteger)totalUnreadCount{
  148. [self youpaifupdateUnread];
  149. }
  150. - (void)didRemoveRecentSession:(NIMRecentSession *)recentSession
  151. totalUnreadCount:(NSInteger)totalUnreadCount{
  152. [self youpaifupdateUnread];
  153. }
  154. - (void)messagesDeletedInSession:(NIMSession *)session{
  155. [self youpaifupdateUnread];
  156. }
  157. - (void)allMessagesDeleted{
  158. [self youpaifupdateUnread];
  159. }
  160. #pragma mark - setup
  161. - (void)youpaifsetUpAllChildVc {
  162. // if ([LCSaveData getWhiteVersion]) {
  163. //
  164. //// YOUPAIHRChatRoomHomeVC *liveVC = [[YOUPAIHRChatRoomHomeVC alloc] init];
  165. //// [self setupOneChildVcWithVc:liveVC image:@"vqu_images_tab_party_s" selectedImage:@"vqu_images_tab_party_n" title:@"派对"];
  166. //
  167. //
  168. // YOUPAILZHomeVC *home = [[YOUPAILZHomeVC alloc] init];
  169. // [self setupOneChildVcWithVc:home image:@"vqu_images_tab_fate_n" selectedImage:@"vqu_images_tab_fate_s" title:@"缘分"];
  170. //
  171. // YOUPAILZDiscoverVC *discover = [[YOUPAILZDiscoverVC alloc] init];
  172. // [self setupOneChildVcWithVc:discover image:@"vqu_images_tab_dynamic_n" selectedImage:@"vqu_images_tab_dynamic_s" title:@"动态"];
  173. //
  174. // YOUPAIBBMsgVC *chat =[[YOUPAIBBMsgVC alloc]init];
  175. // [self setupOneChildVcWithVc:chat image:@"vqu_images_tab_chat_n-1" selectedImage:@"vqu_images_tab_chat_s-1" title:@"消息"];
  176. //
  177. // YOUPAIBBProfileVC *mineVc = [[YOUPAIBBProfileVC alloc] init];
  178. // [self setupOneChildVcWithVc:mineVc image:@"vqu_images_tab_my_n-1" selectedImage:@"vqu_images_tab_my_s-1" title:@"我的"];
  179. //
  180. // }else{
  181. // YOUPAIHRChatRoomHomeVC *liveVC = [[YOUPAIHRChatRoomHomeVC alloc] init];
  182. // [self setupOneChildVcWithVc:liveVC image:@"vqu_images_tab_party_s" selectedImage:@"vqu_images_tab_party_n" title:@"派对"];
  183. YMHomePageViewModel *homeVM = [[YMHomePageViewModel alloc]initWithParams:@{}];
  184. // // 女号
  185. // if (homeVM.isFemaleGender) {
  186. // YMHomeController *homeVC = [[YMHomeController alloc]initWithViewModel:homeVM];
  187. // [self setupOneChildVcWithVc:homeVC image:@"ym_tabbar_home_page_normal_icon" selectedImage:@"ym_tabbar_home_page_selected_icon" title:@"首页"];
  188. // }
  189. // // 男号
  190. // else {
  191. // }
  192. YMHomePageViewController *homeVC = [[YMHomePageViewController alloc]initWithViewModel:homeVM];
  193. [self setupOneChildVcWithVc:homeVC image:@"ym_tabbar_home_page_normal_icon" selectedImage:@"ym_tabbar_home_page_selected_icon" title:@"首页"];
  194. // YOUPAILZHomeVC *home = [[YOUPAILZHomeVC alloc] init];
  195. // [self setupOneChildVcWithVc:home image:@"vqu_images_tab_fate_n" selectedImage:@"vqu_images_tab_fate_s" title:@"缘分"];
  196. // YMDynamicViewModel *dynamicVM = [[YMDynamicViewModel alloc] initWithParams:@{}];
  197. // YMDynamicViewController *dynamicVC = [[YMDynamicViewController alloc] initWithViewModel:dynamicVM];
  198. // [self setupOneChildVcWithVc:dynamicVC image:@"ym_tabbar_dynamic_normal_icon" selectedImage:@"ym_tabbar_dynamic_selected_icon" title:@"动态"];
  199. YOUPAILZDiscoverVC *discover = [[YOUPAILZDiscoverVC alloc] init];
  200. [self setupOneChildVcWithVc:discover image:@"ym_tabbar_dynamic_normal_icon" selectedImage:@"ym_tabbar_dynamic_selected_icon" title:@"动态"];
  201. YMRankingViewModel *rankingVM = [[YMRankingViewModel alloc]initWithParams:@{}];
  202. YMRankingViewController *rankingVC = [[YMRankingViewController alloc]initWithViewModel:rankingVM];
  203. [self setupOneChildVcWithVc:rankingVC image:@"ym_tabbar_ranking_normal_icon" selectedImage:@"ym_tabbar_ranking_selected_icon" title:@"榜单"];
  204. // YOUPAILCBoardVC *board = [[YOUPAILCBoardVC alloc]init];
  205. // board.youpaiptype = 0;
  206. // board.youpaiphiddenBack = YES;
  207. // [self setupOneChildVcWithVc:board image:@"vqu_images_tab_rank_n" selectedImage:@"vqu_images_tab_rank_s" title:@"榜单"];
  208. // YOUPAILZLiveVC *liveVC = [[YOUPAILZLiveVC alloc] init];
  209. // ZCBaseWebVC* baseWeb = [[ZCBaseWebVC alloc]init];
  210. // baseWeb.contentUrl = [LCSaveData getRankH5Url];
  211. // [LCHttpHelper requestWithURLString:AppConfig parameters:nil needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  212. // NSDictionary* dict = (NSDictionary*)responseObject;
  213. // NSInteger code = [[dict objectForKey:@"code"] integerValue];
  214. // if (code==0) {//成功
  215. // NSString *rankH5Url = [[dict objectForKey:@"data"]objectForKey:@"rank_url_full"];
  216. // [LCSaveData saveRankH5Url:rankH5Url];
  217. // baseWeb.contentUrl = [LCSaveData getRankH5Url];
  218. // [baseWeb reloadWebView];
  219. // }
  220. // } failure:^(NSError *error) {
  221. //
  222. // }];
  223. // baseWeb.needHideNav = YES;
  224. // [self setupOneChildVcWithVc:baseWeb image:@"vqu_images_tabbar_board_n" selectedImage:@"vqu_images_tabbar_board_s" title:@"榜单"];
  225. // YOUPAILCMessageVC *chat = [[YOUPAILCMessageVC alloc]init];
  226. //modify by leo 20191030
  227. YOUPAILCMsgVC *chat =[[YOUPAILCMsgVC alloc]init];
  228. [self setupOneChildVcWithVc:chat image:@"ym_tabbar_messages_normal_icon" selectedImage:@"ym_tabbar_messages_selected_icon" title:@"消息"];
  229. YMMineViewModel *mineVM = [[YMMineViewModel alloc]initWithParams:@{}];
  230. YMMineViewController *mineVC = [[YMMineViewController alloc]initWithViewModel:mineVM];
  231. [self setupOneChildVcWithVc:mineVC image:@"ym_tabbar_mine_normal_icon" selectedImage:@"ym_tabbar_mine_selected_icon" title:@"我的"];
  232. // YOUPAILZProfileVC *mineVc = [[YOUPAILZProfileVC alloc] init];
  233. // [self setupOneChildVcWithVc:mineVc image:@"vqu_images_tab_my_n-1" selectedImage:@"vqu_images_tab_my_s-1" title:@"我的"];
  234. // }
  235. }
  236. - (void)setupOneChildVcWithVc:(UIViewController *)VC image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title {
  237. ZCBaseNavigationVC *nav = [[ZCBaseNavigationVC alloc] initWithRootViewController:VC];
  238. VC.tabBarItem.title = title;
  239. VC.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  240. VC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  241. // VC.tabBarItem.image = [UIImage imageNamed:image];
  242. // VC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
  243. VC.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -4);
  244. [self addChildViewController:nav];
  245. NSLog(@"%@",self.viewControllers);
  246. }
  247. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
  248. NSInteger index = [self.tabBar.items indexOfObject:item];
  249. self.youpaipcurrentIndex = index;
  250. NSLog(@"index === %ld",index);
  251. // if (index==2) {
  252. // [self.tabbar youpaifupdateCenterBtn:YES];
  253. // }else{
  254. // [self.tabbar youpaifupdateCenterBtn:NO];
  255. // }
  256. // if (self.indexFlag != index) {
  257. // [self youpaifanimationWithIndex:index];
  258. // }
  259. //MARK: 埋点
  260. if (index == 0) {
  261. [UMengRecordTool umengEventCountWithId:FrontPage];
  262. }
  263. else if (index == 1) {
  264. [UMengRecordTool umengEventCountWithId:EnterDynamicPage];
  265. }else if (index == 2){
  266. }else if (index == 3){
  267. [UMengRecordTool umengEventCountWithId:EnterMessageList];
  268. }
  269. else if (index == 4){
  270. [UMengRecordTool umengEventCountWithId:EnterMyPage];
  271. }
  272. if (index == 1 && kAppDelegate.isDisplayVideoDynamicPage) {
  273. // self.tabbar.tintColor = LCWhiteColor;
  274. // if (@available(iOS 10.0, *)) {
  275. // self.tabbar.unselectedItemTintColor = HexColorFromRGBA(0xffffff,0.38);
  276. // } else {
  277. //
  278. // }
  279. [self.youpaiptabbar youpaifupdateCenterBtn:YES];
  280. }else{
  281. // self.tabbar.tintColor = HexColorFromRGB(0xA7A9FD);
  282. // if (@available(iOS 10.0, *)) {
  283. //
  284. // self.tabbar.unselectedItemTintColor = LCTextNormal;
  285. //
  286. // } else {
  287. //
  288. // }
  289. [self.youpaiptabbar youpaifupdateCenterBtn:NO];
  290. }
  291. self.youpaiptabbar.backgroundColor = [UIColor whiteColor];
  292. }
  293. - (void)youpaifanimationWithIndex:(NSInteger) index {
  294. NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
  295. for (UIView *tabBarButton in self.tabBar.subviews) {
  296. if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  297. [tabbarbuttonArray addObject:tabBarButton];
  298. }
  299. }
  300. CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  301. pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  302. pulse.duration = 0.12;
  303. pulse.repeatCount= 1;
  304. pulse.autoreverses= NO;
  305. pulse.fromValue= [NSNumber numberWithFloat:0.9];
  306. pulse.toValue= [NSNumber numberWithFloat:1.0];
  307. UIView *tabBarButton = [tabbarbuttonArray objectAtIndex:index];
  308. [[tabBarButton layer] addAnimation:pulse forKey:nil];
  309. //MODIFY BY LEO FIX BUG IOS13 --20191008
  310. /*
  311. [[tabbarbuttonArray[index] layer]
  312. addAnimation:pulse forKey:nil];
  313. */
  314. self.youpaipindexFlag = index;
  315. }
  316. #pragma mark 修复严重内存泄漏3
  317. -(void)dealloc
  318. {
  319. NSLog(@"TabBarVC delloc");
  320. }
  321. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  322. // NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];
  323. //
  324. // UIButton *tabBarBtn = tabBarController.tabBar.subviews[index+1];
  325. // UIImageView *imageView = tabBarBtn.subviews.firstObject;
  326. //
  327. // // 我们把动画加到这个imageView上,即可实现动画效果
  328. //
  329. // // ......
  330. //双击那个item
  331. // if (tabBarController.selectedIndex == 1) {
  332. // self.youpaiptabbar.backgroundColor = HexColorFromRGB(0x2A2935);
  333. // if ([self youpaifcheckIsDoubleClick:viewController]) {
  334. // [[NSNotificationCenter defaultCenter] postNotificationName:@"homeToTop" object:nil];//首页置顶
  335. // }
  336. // }
  337. // if (tabBarController.selectedIndex == 2) {
  338. // self.youpaiptabbar.backgroundColor = HexColorFromRGB(0x2A2935);
  339. // if ([self youpaifcheckIsDoubleClick:viewController]) {
  340. // if (kAppDelegate.isDisplayVideoDynamicPage) {//视频
  341. // [[NSNotificationCenter defaultCenter] postNotificationName:@"videoToTop" object:nil];//视频置顶
  342. // }else{//动态
  343. // self.youpaiptabbar.backgroundColor = HexColorFromRGB(0x2A2935);
  344. // [[NSNotificationCenter defaultCenter] postNotificationName:@"dynamicToTop" object:nil];//动态置顶
  345. // }
  346. //
  347. // }
  348. // }
  349. return YES;
  350. }
  351. - (BOOL)youpaifcheckIsDoubleClick:(UIViewController *)viewController
  352. {
  353. static UIViewController *lastViewController = nil;
  354. static NSTimeInterval lastClickTime = 0;
  355. if (lastViewController != viewController) {
  356. lastViewController = viewController;
  357. lastClickTime = [NSDate timeIntervalSinceReferenceDate];
  358. return NO;
  359. }
  360. NSTimeInterval clickTime = [NSDate timeIntervalSinceReferenceDate];
  361. if (clickTime - lastClickTime > 3 ) {
  362. lastClickTime = clickTime;
  363. return NO;
  364. }
  365. lastClickTime = clickTime;
  366. return YES;
  367. }
  368. @end