YOUPAILCTabBarVC.m 17 KB

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