dkahgld

ZBTabController.m 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // ZBTabController.m
  3. // ZBProject
  4. //
  5. // Created by 学丽 on 2019/3/26.
  6. // Copyright © 2019 ZB. All rights reserved.
  7. //
  8. #import "ZBTabController.h"
  9. #import "ZBNavigationController.h"
  10. @interface ZBTabController ()
  11. @property (nonatomic,assign) NSInteger indexFlag;
  12. @end
  13. @implementation ZBTabController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. [self setUpOneChildVcWithVc:[[OrderViewController alloc] init] Image:@"tab_order_n" selectedImage:@"tab_order_s" title:@"首页"];
  17. [self setUpOneChildVcWithVc:[[ProductViewController alloc] init] Image:@"tab_data_n" selectedImage:@"tab_data_s" title:@"选品"];
  18. [self setUpOneChildVcWithVc:[[DataViewController alloc] init] Image:@"tab_sy_n" selectedImage:@"tab_sy_s" title:@"收益"];
  19. // [self setUpOneChildVcWithVc:[[MineViewController alloc] init] Image:@"tab_home_n" selectedImage:@"tab_home_s" title:@"我的"];
  20. }
  21. - (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
  22. {
  23. [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor baseColor]} forState:UIControlStateSelected];
  24. ZBNavigationController *nav = [[ZBNavigationController alloc] initWithRootViewController:Vc];
  25. UIImage *myImage = [UIImage imageNamed:image];
  26. myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  27. Vc.title=title;
  28. Vc.tabBarItem.image = myImage;
  29. UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
  30. mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  31. Vc.tabBarItem.selectedImage = mySelectedImage;
  32. Vc.tabBarItem.title = title;
  33. Vc.navigationItem.title = title;
  34. [self addChildViewController:nav];
  35. }
  36. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
  37. NSInteger index = [self.tabBar.items indexOfObject:item];
  38. if (index != self.indexFlag) {
  39. //执行动画
  40. NSMutableArray *arry = [NSMutableArray array];
  41. for (UIView *btn in self.tabBar.subviews) {
  42. if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  43. [arry addObject:btn];
  44. }
  45. }
  46. //添加动画
  47. //---将下面的代码块直接拷贝到此即可---
  48. self.indexFlag = index;
  49. [self clickAnimationWithIndex:index arr:arry];
  50. }
  51. }
  52. - (void)clickAnimationWithIndex:(NSInteger)index arr:(NSArray *)arry{
  53. //放大效果,并回到原位
  54. CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  55. //速度控制函数,控制动画运行的节奏
  56. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  57. animation.duration = 0.2; //执行时间
  58. animation.repeatCount = 1; //执行次数
  59. animation.autoreverses = YES; //完成动画后会回到执行动画之前的状态
  60. animation.fromValue = [NSNumber numberWithFloat:0.7]; //初始伸缩倍数
  61. animation.toValue = [NSNumber numberWithFloat:1.1]; //结束伸缩倍数
  62. [[arry[index] layer] addAnimation:animation forKey:nil];
  63. }
  64. @end