省钱达人老版本

YHTabbarController.m 4.3KB

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