日常記錄工程中(公司產(chǎn)品日常抄淘寶)遇到的問題~~
先上效果圖(原諒我沒有GIF的圖):
解釋說(shuō)明:第一個(gè)tabbar點(diǎn)擊時(shí)“雞頭”順時(shí)針旋轉(zhuǎn)一圈,其余tabbar點(diǎn)擊時(shí)抖動(dòng)效果(真的是仿淘寶的)……
→ 好了開始上代碼↓:
創(chuàng)建自定義的 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 = @[@"首頁(yè)",@"分類",@"發(fā)現(xiàn)中心",@"購(gòu)物車",@"個(gè)人中心"];
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{
//設(shè)置title和圖片
//**特別說(shuō)明**:見代碼后面
vc.tabBarItem.title = @"";
vc.tabBarItem.image = [[UIImage imageNamed:image]imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
// //設(shè)置文字位置揍异,在有文字時(shí)用于調(diào)整文字的位置
// [vc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -2.5)];
//設(shè)置圖片位置 注意:圖片 上下 or 左右 的間距要一樣馅而,否側(cè)有些手機(jī)上會(huì)變形
[vc.tabBarItem setImageInsets:UIEdgeInsetsMake(7, 0, -7, 0)]; //
//添加到
[self addChildViewController:vc];
}
#pragma mark - UITabBarControllerDelegate
//點(diǎn)擊下面的tabBar的按鈕時(shí)候,根據(jù)BOOL值來(lái)判斷是否處于可繼續(xù)點(diǎn)擊狀態(tài)
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
//此方法里我用來(lái)判斷在用戶點(diǎn)擊“個(gè)人中心”時(shí)是否登錄
return YES;
}
//此方法系統(tǒng)自動(dòng)調(diào)用徒河,寫在繼承自UITabBarController的controller中
//設(shè)置點(diǎn)擊的動(dòng)畫----本次重點(diǎn)
-(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) { //順時(shí)針旋轉(zhuǎn)效果
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.duration = 0.3;
//如果這里想設(shè)置成一直自旋轉(zhuǎn)刚照,可以設(shè)置為MAXFLOAT宙地,否則設(shè)置具體的數(shù)值則代表執(zhí)行多少次
animation.repeatCount = 1;
animation.autoreverses = NO;
//默認(rèn)是順時(shí)針效果摔认,若將fromValue和toValue的值互換,則為逆時(shí)針效果
animation.fromValue = [NSNumber numberWithFloat:0.f];
animation.toValue = [NSNumber numberWithFloat: M_PI *2];
[[tabbarbuttonArray[index] layer] addAnimation:animation forKey:nil];
}else{ //抖動(dòng)效果
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里跟系統(tǒng)的一樣使用
SLMainTabbarVC *tabbar = [[SLMainTabbarVC alloc] init];
_tabBarVC = tabbar;
self.window.rootViewController = tabbar;
特別說(shuō)明:
→1宅粥、由于第一個(gè)tabbar的“雞頭”不能顯示“首頁(yè)”文字参袱,所以我這里的tabbar沒有文字,都是image和title在一起的圖片秽梅,但是要注意抹蚀,此時(shí)的NavigationController的RootViewController的導(dǎo)航文字不要這樣 vc.title = @"首頁(yè)";設(shè)置企垦,因?yàn)檫@是tabbar的item.title在當(dāng)前VC顯示時(shí)對(duì)應(yīng)的tabbar還是會(huì)顯示 vc.title 的文字环壤。
如果vc的title必須設(shè)置,可以用下面方法代替:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UILabel *label = [UILabel new];
label.frame = CGRectMake(0, 0, 100, 40); //防止有些版本手機(jī)不顯示title的情況钞诡,設(shè)置一個(gè)frame
label.textAlignment = NSTextAlignmentCenter;
label.text = @"發(fā)現(xiàn)";
label.textColor = [UIColor blackColor];
label.font = [UIFont systemFontOfSize:17];
self.navigationItem.titleView = label;
}
→2郑现、item.image和item.image分開的情況時(shí)的做法跟現(xiàn)在的一樣樣,在設(shè)置addChildViewController方法里 vc.tabBarItem.title = titie; 就可以了荧降。此時(shí)的情況本寶寶只試過(guò)抖動(dòng)的效果接箫,別的效果沒有試過(guò),同學(xué)們可以試著玩玩朵诫,指不定就玩出了別的花樣O(∩_∩)O~
對(duì)于tabar的一些延伸
1辛友、tabBarItem的image&title的改變
要求:在用戶登錄成功后換掉第2個(gè)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修改了圖片之后剪返,會(huì)影響subviews的順序废累。
//修改了第二個(gè)的圖片,會(huì)導(dǎo)致第二個(gè)UITabBarButton在subviews變到最后一個(gè)脱盲,所以根據(jù)位置重新排下序
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;
}];
//后面內(nèi)容和上面一樣
}