iOS Tabbar中间添加凸起可旋转按钮功能

2020-01-21 03:01:23于海丽

最近的项目中有需求在tabbar中间添加凸起按钮,并且点击时按钮要旋转,看了仿斗鱼的凸起,点击后是present出来View,而不是像常规的tabbar上添加一个页面,所以不符合要求,经过一段摸索最后得的一个比较好的效果,下面看效果图

ios,tabbar,凸起可旋转按钮,旋转按钮

![效果图.gif]

##需求分析

* tabbar有5个item,每个对应一个页面

* 中间item为凸起按钮

* 中间按钮点击后旋转

##效果实现

* 设置5个item

我们一步步来解决这个问题,首先创建MCTabBarController继承UITabBarController,然后和常规一样创建5个item,中间的按钮不设置图片,代码如下


//MCTabBarController.m
//添加子控制器
- (void)addChildViewControllers{
 //图片大小建议32*32
 [self addChildrenViewController:[[ViewController alloc] init] andTitle:@"首页" andImageName:@"tab1_n" andSelectImage:@"tab1_p"];
 [self addChildrenViewController:[[ViewController alloc] init] andTitle:@"扩展" andImageName:@"tab2_n" andSelectImage:@"tab2_p"];
 //中间这个不设置东西,只占位
 [self addChildrenViewController:[[ViewController alloc] init] andTitle:@"旋转" andImageName:@"" andSelectImage:@""];
 [self addChildrenViewController:[[ViewController alloc] init] andTitle:@"发现" andImageName:@"tab3_n" andSelectImage:@"tab3_p"];
 [self addChildrenViewController:[[ViewController alloc] init] andTitle:@"我" andImageName:@"tab4_n" andSelectImage:@"tab4_p"];
}
- (void)addChildrenViewController:(UIViewController *)childVC andTitle:(NSString *)title andImageName:(NSString *)imageName andSelectImage:(NSString *)selectedImage{
 childVC.tabBarItem.image = [UIImage imageNamed:imageName];
 childVC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
 childVC.title = title;
 
 BaseNavigationController *baseNav = [[BaseNavigationController alloc] initWithRootViewController:childVC];
 
 [self addChildViewController:baseNav];
}

这样实现的效果如下图所示

ios,tabbar,凸起可旋转按钮,旋转按钮

[图一.png]

* 添加凸起按钮

我们可以在UITabBar上添加我们的凸起按钮,让他的位置在没有设置的中间按钮偏上,按钮的点击和中间按钮点击绑定,这里直接在MCTabBarController.m中添加会有问题

1、因为凸起按钮的frame超出了UITabBar的frame,这样超出的区域点击按钮会没有响应(图二红框区域),原因和解决办法详情参考我的这篇[iOS UIButton 点击无响应的解决办法],由于要在UITabBar上添加凸起按钮,并且处理点击无效的问题,所以这里创建了MCTabBar继承UITabBar