iOS开发小技巧:TabBar

2,027 阅读3分钟

封装TabBarViewController

www.jianshu.com/p/0496c5caf…

总结

www.jianshu.com/p/f1b0be2c9…

去除tabbar上方的灰线

[[UITabBar appearance] setShadowImage:[UIImage new]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];

重复点击tabbarItem刷新当前页

AppDelegate来做代理,接收到点击事件后,通过通知中心发送通知。 AppDelegate遵守协议UITabbarControllerDelegate,实现其代理方法 点击回调。

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    // 发通知
}

哪个控制器需要刷新,就监听通知。 imagddddddde.png

非根VC下, 处理tabbar的显示隐藏

重写自定义的UINavigationController中的push方法

-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
     if (self.childViewControllers.count==1) {
         viewController.hidesBottomBarWhenPushed = YES; //viewController是将要被push的控制器
     }
     [super pushViewController:viewController animated:animated];
}

设置TabBarItem的字体大小

[childVC.tabBarItem setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:13.0] } forState:UIControlStateNormal];

TabBar的字体颜色

 item = [item initWithTitle:titleStr image:unselectedImage selectedImage:selectedImage];
        
        NSDictionary *dicNormal = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
        NSDictionary *dicSelected = @{NSForegroundColorAttributeName:[UIColor redColor]};
        
        [item setTitleTextAttributes:dicNormal forState:UIControlStateNormal];
        [item setTitleTextAttributes:dicSelected forState:UIControlStateSelected];

设置tabBar的颜色

    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor = mainGrayColor;
    [self.tabBar insertSubview:bgView atIndex:0];
    self.tabBar.opaque = YES;

通过代码来切换VC

//  如果当前类是TabBarController,直接执行以下实例方法
    [self setSelectedIndex:3];

TabBar点击Item的动画效果

www.cnblogs.com/yajunLi/p/6… 原理:利用UITabBarController实现,在tabbar的 didSelectItem 代理里添加动画效果。

@interface MainTabbarVC ()<UITabBarControllerDelegate>

@property (nonatomic,assign) NSInteger  indexFlag;  
//记录上一次点击tabbar的Item,使用时记得先在viewDidLoad初始化 = 0

@end

//  代理方法-点击TabBar Item时调用
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    //  当前点击的Item 的 index
    NSInteger index = [self.tabBar.items indexOfObject:item];

    //对比,判断。如果点击的不是同上一个按钮,则执行动画
    if (index != self.indexFlag) {
        
        // 把TabBar的子控件UITabBarButton添加到数组 暂存
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.tabBar.subviews) {
            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                 [arry addObject:btn];
            }
        }
        //添加动画---将下面的代码块直接拷贝到此即可---
        ... ...
        
        self.indexFlag = index;   // 保存index
    }
}

1.先放大再缩小

//放大效果,并回到原位
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//速度控制函数,控制动画运行的节奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;       //执行时间
animation.repeatCount = 1;      //执行次数
animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
[[arry[index] layer] addAnimation:animation forKey:nil];

2.Z轴旋转

//z轴旋转180度
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//速度控制函数,控制动画运行的节奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;       //执行时间
animation.repeatCount = 1;      //执行次数
animation.removedOnCompletion = YES;
animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
animation.toValue = [NSNumber numberWithFloat:M_PI];     //结束伸缩倍数
[[arry[index] layer] addAnimation:animation forKey:nil];

3.Y轴位移

//向上移动
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
//速度控制函数,控制动画运行的节奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;       //执行时间
animation.repeatCount = 1;      //执行次数
animation.removedOnCompletion = YES;
animation.fromValue = [NSNumber numberWithFloat:0];   //初始伸缩倍数
animation.toValue = [NSNumber numberWithFloat:-10];     //结束伸缩倍数
[[arry[index] layer] addAnimation:animation forKey:nil];

4.放大并保持

//放大效果
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//速度控制函数,控制动画运行的节奏
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.duration = 0.2;       //执行时间
animation.repeatCount = 1;      //执行次数
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;           //保证动画效果延续
animation.fromValue = [NSNumber numberWithFloat:1.0];   //初始伸缩倍数
animation.toValue = [NSNumber numberWithFloat:1.15];     //结束伸缩倍数
[[arry[index] layer] addAnimation:animation forKey:nil];
//移除其他tabbar的动画
for (int i = 0; i<arry.count; i++) {
    if (i != index) {
        [[arry[i] layer] removeAllAnimations];
    }
}

滚动时TabBar位移动画

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [UIView animateWithDuration:1 animations:^{
        self.tabBarController.tabBar.transform = CGAffineTransformMakeTranslation(0, 49);
    }];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [UIView animateWithDuration:1 animations:^{
        self.tabBarController.tabBar.transform = CGAffineTransformIdentity;
    }];
}