我的产品需求:
我需要在最中间的TabBarItem实现点击后悬浮,并且将图标换成发布动态的按钮,用户再次点击这个TabBarItem进入发布动态的界面。
下图是APP的结构图,帮助理解
下图是未选中动态的效果
下图是选中动态的效果,并变成发布动态,简而言之,用户再次点击时,将会进入发布动态的界面。
代码前提:
初始工作,我已经在Main.storyboard设置好了,这部分就不写了,来个小截图
再来一段TabBarItem选中和未选中颜色切换的代码
// 1.获得RGB颜色
#define YTHColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define YTHColorAlpha(r, g, b ,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
/**
tabBarItem的字体颜色设置
*/
-(void)setTabBarItemFontColor{
// 字体颜色 (选中)
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0F], NSForegroundColorAttributeName : YTHColor(254, 45, 121)} forState:UIControlStateSelected];
// 字体颜色 (未选中)
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12.0F], NSForegroundColorAttributeName:YTHColor(101, 101, 101)} forState:UIControlStateNormal];
}
具体的代码实现
首先在AppDelegate遵循UITabBarControllerDelegate协议
@interface AppDelegate ()<UITabBarControllerDelegate>
UITabBarController *baseTabBar = (UITabBarController *)self.window.rootViewController;
baseTabBar.delegate=self;
重写协议方法
#pragma mark -UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex==2) {
viewController.tabBarItem.title=@"发布";
viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(-6, 0, 6, 0);
}else{
UIViewController *TviewController=tabBarController.viewControllers[2];
TviewController.tabBarItem.title=@"动态";
TviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
}
那么,同一个TabBarItem功能切换是如何实现的?
#pragma mark -UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DynamicViewController" object:nil userInfo:nil];
if (tabBarController.selectedIndex==2) {
viewController.tabBarItem.title=@"发布";
viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(-6, 0, 6, 0);
}else{
UIViewController *TviewController=tabBarController.viewControllers[2];
TviewController.tabBarItem.title=@"动态";
TviewController.tabBarItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
}
}
准确的说,就是发送通知!我写在DynamicViewController中的,所以要去DynamicViewController接收通知了!
DynamicViewController.m(我删除了无关的代码)
@property (nonatomic, assign) NSInteger lastSelectedIndex;//上次选中的索引(或者控制器)
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarSeleted) name:@"DynamicViewController" object:nil];
}
/**
接收到通知实现方法
*/
- (void)tabBarSeleted {
// 如果是连续选中2次
if (self.lastSelectedIndex == self.tabBarController.selectedIndex && [self isShowingOnKeyWindow]) {
//跳转到发布界面
productInformationViewController *productInformationVC=[[productInformationViewController alloc] init];
[self.navigationController pushViewController:productInformationVC animated:YES];
}
// 记录当前选中的索引
self.lastSelectedIndex = self.tabBarController.selectedIndex;
}
/**
判断一个控件是否真正显示在主窗口
*/
- (BOOL)isShowingOnKeyWindow{
// 主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// 以主窗口左上角为坐标原点, 计算self的矩形框
CGRect newFrame = [keyWindow convertRect:self.view.frame fromView:self.view.superview];
CGRect winBounds = keyWindow.bounds;
// 主窗口的bounds 和 self的矩形框 是否有重叠
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
return !self.view.isHidden && self.view.alpha > 0.01 && self.view.window == keyWindow && intersects;
}