iOS 自定义仿淘宝Tabbar
2018-05-17 本文已影响666人
莦婼姑娘
日常记录工程中(公司产品日常抄淘宝)遇到的问题~~
线上效果图(原谅我没有GIF的图):
Tabbar效果图2
解释说明:第一个tabbar点击时“鸡头”顺时针旋转一圈,其余tabbar点击时抖动效果(真的是仿淘宝的)……
→ 好了开始上代码↓:
创建自定义的 SLMainTabbarVc
//SLMainTabbarVC.h 文件
#import <UIKit/UIKit.h>
#import "SLTabBarViewController.h"
@interface SLMainTabbarVC : SLTabBarViewController
@end
//-------------------------------
//SLMainTabbarVC.m 文件
#import "SLMainTabbarVC.h"
#import "ViewController.h"
@implementation SLMainTabbarVC
-(instancetype)init{
if (self = [super init]) {
[self createUI];
self.delegate = self;
}
return self;
}
-(void)createUI{
ViewController *vc1 = [[ViewController alloc] init];
vc1.hidesBottomBarWhenPushed = NO;
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
ViewController *vc2 = [[ViewController alloc] init];
vc2.hidesBottomBarWhenPushed = NO;
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
ViewController *vc3 = [[ViewController alloc] init];
vc3.hidesBottomBarWhenPushed = NO;
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:vc3];
ViewController *vc4 = [[ViewController alloc] init];
vc4.hidesBottomBarWhenPushed = NO;
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];
ViewController *vc5 = [[ViewController alloc] init];
vc5.hidesBottomBarWhenPushed = NO;
UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:vc5];
NSArray *vc = @[nav1,nav2,nav3,nav4,nav5];
NSArray *title = @[@"首页",@"分类",@"发现中心",@"购物车",@"个人中心"];
NSArray *image = @[@"tab_1_n-",@"tab_2_n-",@"tab_3_n-",@"tab_4_n-",@"tab_5_n-"];
NSArray *selectImage = @[@"tab_1_h-",@"tab_2_h-",@"tab_3_h-",@"tab_4_h-",@"tab_5_h-"];
for (int i = 0; i<vc.count; i++) {
[self addChildViewController:vc[i] andTitle:title[i] image:image[i] selectImage:selectImage[i]];
}
}
-(void)addChildViewController:(UIViewController *)vc andTitle:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage{
//设置title和图片
//**特别说明**:见代码后面
vc.tabBarItem.title = @"";
vc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
// //设置文字位置,在有文字时用于调整文字的位置
// [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2.5)];
//设置图片位置 注意:图片 上下 or 左右 的间距要一样,否侧有些手机上会变形
[vc.tabBarItem setImageInsets:UIEdgeInsetsMake(7, 0, -7, 0)]; //
//添加到
[self addChildViewController:vc];
}
#pragma mark - UITabBarControllerDelegate
//点击下面的tabBar的按钮时候,根据BOOL值来判断是否处于可继续点击状态
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
//此方法里我用来判断在用户点击“个人中心”时是否登录
return YES;
}
//此方法系统自动调用,写在继承自UITabBarController的controller中
//设置点击的动画----本次重点
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSInteger index = [self.tabBar.items indexOfObject:item];
NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
for (UIView *tabBarButton in self.tabBar.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabbarbuttonArray addObject:tabBarButton];
}
}
if (index == 0) { //顺时针旋转效果
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 0.3;
//如果这里想设置成一直自旋转,可以设置为MAXFLOAT,否则设置具体的数值则代表执行多少次
animation.repeatCount = 1;
animation.autoreverses = NO;
//默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果
animation.fromValue = [NSNumber numberWithFloat:0.f];
animation.toValue = [NSNumber numberWithFloat: M_PI *2];
[[tabbarbuttonArray[index] layer] addAnimation:animation forKey:nil];
}else{ //抖动效果
CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulse.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulse.duration = 0.1;
pulse.repeatCount = 1;
pulse.autoreverses = YES;
pulse.fromValue = [NSNumber numberWithFloat:0.7];
pulse.toValue = [NSNumber numberWithFloat:1.3];
[[tabbarbuttonArray[index] layer] addAnimation:pulse forKey:nil];
}
}
//-------------------------------
//在AppDelegate里跟系统的一样使用
SLMainTabbarVC *tabbar = [[SLMainTabbarVC alloc] init];
_tabBarVC = tabbar;
self.window.rootViewController = tabbar;
特别说明:
→1、由于第一个tabbar的“鸡头”不能显示“首页”文字,所以我这里的tabbar没有文字,都是image和title在一起的图片,但是要注意,此时的NavigationController的RootViewController的导航文字不要这样 vc.title = @"首页";设置,因为这是tabbar的item.title在当前VC显示时对应的tabbar还是会显示 vc.title 的文字。
如果vc的title必须设置,可以用下面方法代替:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UILabel *label = [UILabel new];
label.frame = CGRectMake(0, 0, 100, 40); //防止有些版本手机不显示title的情况,设置一个frame
label.textAlignment = NSTextAlignmentCenter;
label.text = @"发现";
label.textColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:17];
self.navigationItem.titleView = label;
}
→2、item.image和item.image分开的情况时的做法跟现在的一样样,在设置addChildViewController方法里 vc.tabBarItem.title = titie; 就可以了。此时的情况本宝宝只试过抖动的效果,别的效果没有试过,同学们可以试着玩玩,指不定就玩出了别的花样O(∩_∩)O~
对于tabar的一些延伸
1、tabBarItem的image&title的改变
要求:在用户登录成功后换掉第2个tabBarItem的image,在SLMainTabbarVC.m 文件里接受通知:
//登录成功后通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theLogin) name:LoginSuccessfullNoticetion object:nil];
//方法
-(void)theLogin{
UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
item.image = [[UIImage imageNamed:@"tab_2_d_n-"]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
item.selectedImage = [[UIImage imageNamed:@"tab_2_d_h-"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
}
//代理方法里
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSInteger index = [self.tabBar.items indexOfObject:item];
NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
for (UIView *tabBarButton in self.tabBar.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabbarbuttonArray addObject:tabBarButton];
}
}
//item修改了图片之后,会影响subviews的顺序。
//修改了第二个的图片,会导致第二个UITabBarButton在subviews变到最后一个,所以根据位置重新排下序
tabbarbuttonArray = [tabbarbuttonArray sortedArrayUsingComparator:^NSComparisonResult(UIView *obj1, UIView *obj2) {
NSComparisonResult result = [[NSNumber numberWithFloat:obj1.frame.origin.x] compare:[NSNumber numberWithFloat:obj2.frame.origin.x]];
return result;
}];
//后面内容和上面一样
}