【iOS】Tabbar的item的相关业务场景 及 点击交互的控制

1.更换Tabbar某一item的icon和title
LBTabBarController *tabbarController = (LBTabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        if (tabbarController.viewControllers && tabbarController.viewControllers.count > 0) {
            LBNavigationController *nav = (LBNavigationController *)tabbarController.viewControllers.lastObject;
            NSArray *array = nav.viewControllers;
            if (array && array.count > 0) {
                UIViewController *viewController = array.firstObject;
                //更换icon
                viewController.tabBarItem.title = @"首页";
                viewController.tabBarItem.image = [[UIImage imageNamed:@"ic_home"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
                viewController.tabBarItem.selectedImage = [[UIImage imageNamed:@"ic_home_blue"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
            }
       }

2.获取Tabbar某一item的ImageView,并做相关操作(常做旋转动画)
/**
 获取当前TabBarItem中的ImageView
 
 @param currentViewController 当前ViewController
 @return TabBarItem中的ImageView
 */
- (UIImageView *)getTabBarButtonImageViewWithCurrentVc:(UIViewController *)currentViewController{
    
    UIControl *tabBarButton = [currentViewController.tabBarItem valueForKey:@"view"];
    if (tabBarButton) {
        UIImageView *tabBarSwappableImageView = [tabBarButton valueForKey:@"info"];
        if (tabBarSwappableImageView) {
            return tabBarSwappableImageView;
        }
    }
    return nil;
}

/**
 旋转动画
 
 @return CABasicAnimation 动画
 */
- (CABasicAnimation *)rotationAnimation{
    //指定动画属性
    CABasicAnimation *_rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    //单次动画时间
    _rotationAnimation.duration = 0.7;
    //重复次数
    _rotationAnimation.repeatCount= 99;
    //开始角度
    _rotationAnimation.fromValue = [NSNumber numberWithFloat:0];
    //结束角度
    _rotationAnimation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    // 是否在动画结束后移除动画
    _rotationAnimation.removedOnCompletion = NO;
    
    return _rotationAnimation;
}

/**
 添加旋转动画
 
 @param currentViewController 当前ViewController
 */
- (void)addTabBarButtonRotationAnimationWithCurrentViewController:(UIViewController *)currentViewController{
    
    UIImageView *tabBarSwappableImageView = [self getTabBarButtonImageViewWithCurrentVc:currentViewController];
    
    if (tabBarSwappableImageView) {
        [[tabBarSwappableImageView layer] addAnimation:self.rotationAnimation forKey:@"TabBarButtonTransformRotationAnimationKey"];
        
        //选中和未选中的image都需要更改为刷新中的图,不然会出现正在刷新时切换TabBar导致未选中的图片在旋转
        currentViewController.tabBarItem.title = @"刷新";
        currentViewController.tabBarItem.selectedImage = [[UIImage imageNamed:@"ic_reload"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        currentViewController.tabBarItem.image = [[UIImage imageNamed:@"ic_reload"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

/**
 移除旋转动画
 
 @param currentViewController 当前ViewController
 */
- (void)removeTabBarButtonRotationAnimationWithCurrentViewController:(UIViewController *)currentViewController{
        
    UIImageView *tabBarSwappableImageView = [self getTabBarButtonImageViewWithCurrentVc:currentViewController];
    
    if (tabBarSwappableImageView) {
        
        if ([[tabBarSwappableImageView layer] animationForKey:@"TabBarButtonTransformRotationAnimationKey"]) {
            
            [[tabBarSwappableImageView layer] removeAnimationForKey:@"TabBarButtonTransformRotationAnimationKey"];            
        }
    }
    
    //移除后重新更换选中和未选中的图片
    if ([currentViewController isKindOfClass:[LBSMainViewController class]]) {
        
        currentViewController.tabBarItem.title = @"快讯";
        currentViewController.tabBarItem.image = [[UIImage imageNamed:@"ic_newflash"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
        currentViewController.tabBarItem.selectedImage = [[UIImage imageNamed:@"ic_newflash_blue"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    }
}
3.控制Tabbar点击事件的交互
实现UITabBarControllerDelegate的协议方法 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 可实现控制Tabbar点击事件交互 用于产品的特殊需求吧

eg:首页有两种显示情况A和B,默认显示A,在用户操作的过程中达到某种状态需要切换到B,并把Tabbar的最后一个icon和title更改掉,在B状态下可以点击Tabbar最后一个item可以让页面切换到A,同时Tabbar更换icon和title并正常显示。

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    LBNavigationController *nav = (LBNavigationController *)viewController;
    NSArray *array = nav.viewControllers;
    if (array && array.count > 0) {
        UIViewController *viewController = array.firstObject;
        NSString *title = viewController.tabBarItem.title;
        if ([title isEqualToString:@"首页"]) {

           //切换状态A的操作及更改icon和title(可在别处配置)

            return NO;
        }
    }

    return YES;
}
4. 双击刷新

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 中可以做一些操作监控双击行为并刷新数据。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。