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 中可以做一些操作监控双击行为并刷新数据。

标签:

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术狗|博客
原文地址:《iOS -Tabbar的item的相关业务场景 及 点击交互的控制》 发布于2019-10-13

分享到:
赞(0)

评论 抢沙发

3 + 5 =


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

长按图片转发给朋友

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录