省钱达人

DRTabbarController.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // DRTabbarController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRTabbarController.h"
  9. #import "DRNavigationController.h"
  10. #import "DRHomeMainViewController.h"
  11. #import "DRNineNineMainViewController.h"
  12. #import "DRBrandMainViewController.h"
  13. #import "DRFindMainViewController.h"
  14. #import "DRMineMainViewController.h"
  15. #import "DRCollectionViewController.h"
  16. #import "DRCollectionMainViewController.h"
  17. #import "DRClassifyViewController.h"
  18. #import "DRCommunityViewController.h"
  19. #import "DRCommunityLeftController.h"
  20. @interface DRTabbarController ()<UITabBarControllerDelegate>
  21. @property (nonatomic,assign) NSInteger indexFlag;
  22. @end
  23. @implementation DRTabbarController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self setUpAllChildVc];
  27. }
  28. - (void)setUpAllChildVc
  29. {
  30. [self setUpOneChildVcWithVc:[[DRHomeMainViewController alloc] init] Image:@"tab_home" selectedImage:@"tab_home_selected" title:@"首页"];
  31. // [self setUpOneChildVcWithVc:[[DRNineNineMainViewController alloc] init] Image:@"tab_99" selectedImage:@"tab_99_selected" title:@"9块9"];
  32. // [self setUpOneChildVcWithVc:[[DRBrandMainViewController alloc] init] Image:@"tab_brand" selectedImage:@"tab_brand_selected" title:@"品牌优选"];
  33. [self setUpOneChildVcWithVc:[[DRClassifyViewController alloc] init] Image:@"tab_classify_n" selectedImage:@"tab_classify_s" title:@"分类"];
  34. [self setUpOneChildVcWithVc:[[DRCommunityLeftController alloc] init] Image:@"tab_zhuanqian_n" selectedImage:@"tab_zhuanqian_s" title:@"优选"];
  35. [self setUpOneChildVcWithVc:[[DRMineMainViewController alloc] init] Image:@"tab_mine" selectedImage:@"tab_mine_selected" title:@"我的"];
  36. }
  37. - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
  38. {
  39. [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
  40. DRNavigationController *nav = [[DRNavigationController alloc] initWithRootViewController:Vc];
  41. UIImage *myImage = [UIImage imageNamed:image];
  42. myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  43. Vc.tabBarItem.image = myImage;
  44. UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
  45. mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  46. Vc.tabBarItem.selectedImage = mySelectedImage;
  47. Vc.tabBarItem.title = title;
  48. Vc.navigationItem.title = title;
  49. [self addChildViewController:nav];
  50. }
  51. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
  52. NSInteger index = [self.tabBar.items indexOfObject:item];
  53. if (index != self.indexFlag) {
  54. //执行动画
  55. NSMutableArray *arry = [NSMutableArray array];
  56. for (UIView *btn in self.tabBar.subviews) {
  57. if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  58. [arry addObject:btn];
  59. }
  60. }
  61. //添加动画
  62. //---将下面的代码块直接拷贝到此即可---
  63. self.indexFlag = index;
  64. [self clickAnimationWithIndex:index arr:arry];
  65. }
  66. }
  67. - (void)clickAnimationWithIndex:(NSInteger)index arr:(NSArray *)arry{
  68. //放大效果,并回到原位
  69. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  70. //速度控制函数,控制动画运行的节奏
  71. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  72. animation.duration = 0.2; //执行时间
  73. animation.repeatCount = 1; //执行次数
  74. animation.autoreverses = YES; //完成动画后会回到执行动画之前的状态
  75. animation.fromValue = [NSNumber numberWithFloat:0.7]; //初始伸缩倍数
  76. animation.toValue = [NSNumber numberWithFloat:1.1]; //结束伸缩倍数
  77. [[arry[index] layer] addAnimation:animation forKey:nil];
  78. }
  79. - (void)didReceiveMemoryWarning {
  80. [super didReceiveMemoryWarning];
  81. // Dispose of any resources that can be recreated.
  82. }
  83. /*
  84. #pragma mark - Navigation
  85. // In a storyboard-based application, you will often want to do a little preparation before navigation
  86. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  87. // Get the new view controller using [segue destinationViewController].
  88. // Pass the selected object to the new view controller.
  89. }
  90. */
  91. @end