實(shí)現(xiàn)tabbar點(diǎn)擊的幀動(dòng)畫效果, 可以用下面兩種方法
方法1:
1.在UITabBarController 里面
@interface UITabBarController () <UITabBarControllerDelegate>
//注意數(shù)組是UIImage.CGImage
@property (nonatomic ,strong)NSMutableArray * imagesArray;
@end
2
//UITabBarController
- (void)viewDidLoad {
[super viewDidLoad];
self.imagesArray = [NSMutableArray array];
for (int i = 0; i<4; i++) {
NSMutableArray *images = [NSMutableArray array];
switch (i) {
case 0:
for (int j = 0; j<=10; j++) {
NSString *imageName = [NSString stringWithFormat:@"home.bundle/tabbar_home_sel_%d",j];
[images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
}
break;
case 1:
for (int j = 0; j<=10; j++) {
NSString *imageName = [NSString stringWithFormat:@"center.bundle/tabbar_senter_sel_%d",j];
[images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
}
break;
case 2:
for (int j = 0; j<=10; j++) {
NSString *imageName = [NSString stringWithFormat:@"class.bundle/tabbar_class_sel_%d",j];
[images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
}
break;
case 3:
for (int j = 0; j<=13; j++) {
NSString *imageName = [NSString stringWithFormat:@"me.bundle/tabbar_me_sel_%d",j];
[images addObject:(__bridge UIImage *)[UIImage imageNamed:imageName].CGImage];
}
break;
default:
break;
}
[self.imagesArray addObject:images];
}
3.實(shí)現(xiàn) UITabBarControllerDelegate 方法
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSMutableArray *tabBarBtnArray = [NSMutableArray array];
int index = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
// 獲取UITabBarButton
for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
UIView * tabBarButton = self.tabBar.subviews[i];
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarBtnArray addObject:tabBarButton];
// NSLog(@"tabBarButton.sup=%@",[tabBarButton. class]);
}
}
//獲取當(dāng)前的UITabBarButton
UIView *TabBarButton = tabBarBtnArray[index];
NSArray *images = self.imagesArray[index];
for (UIView *imageV in TabBarButton.subviews) {
if ([imageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
//animation.delegate = self;
animation.values = images;
animation.duration = 0.3;// images.count * 0.08;
animation.calculationMode = kCAAnimationCubic;
[imageV.layer addAnimation:animation forKey:nil];
}
}
}
方法2 , 和方法一區(qū)別不大, 只需修改下面代碼
//注意數(shù)組是UIImage不是UIImage.CGImage
@property (nonatomic ,strong)NSMutableArray * imagesArray;
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSMutableArray *tabBarBtnArray = [NSMutableArray array];
int index = [NSNumber numberWithUnsignedInteger:[tabBar.items indexOfObject:item]].intValue;
// 獲取UITabBarButton
for (int i = 0 ;i < self.tabBar.subviews.count; i++ ) {
UIView * tabBarButton = self.tabBar.subviews[i];
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[tabBarBtnArray addObject:tabBarButton];
// NSLog(@"tabBarButton.sup=%@",[tabBarButton. class]);
}
}
//獲取當(dāng)前的UITabBarButton
UIView *TabBarButton = tabBarBtnArray[index];
NSArray *images = self.imagesArray[index];
for (UIView *imageV in TabBarButton.subviews) {
if ([imageV isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
//圖片是是要UIImage的
UIImageView *imageView = (UIImageView*)imageV;
imageView.animationImages = images;
imageView.animationDuration = images.count * 0.08;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
}
}
}
3.另外你可以設(shè)置tabbar的其他屬性
//1設(shè)置線條顏色(遮擋法)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, SCREEN_WIDTH + 1, 0.5)];
view.backgroundColor = RGBWithAlpha(0xe3e5e9, 1.0);
[[UITabBar appearance] insertSubview:view atIndex:0];
//1.tabbar標(biāo)題顏色
NSDictionary *attributes_normol = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:RGBWithAlpha(0xADADAD, 1)};
NSDictionary *attributes_sel = @{NSFontAttributeName:[UIFont systemFontOfSize:11],NSForegroundColorAttributeName:RGBWithAlpha(0xFF5847, 1)};
for (UITabBarItem *item in self.tabBar.items) {
// item.imageInsets = UIEdgeInsetsMake(-3, 0, 0, 0);
//item.titlePositionAdjustment = UIOffsetMake(0, -4);
[item setTitleTextAttributes:attributes_normol forState:UIControlStateNormal];
[item setTitleTextAttributes:attributes_sel forState:UIControlStateSelected];
}
//假如tabbar的圖片大小不合適, 不想換圖的話可以重新生成一張圖片
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContextWithOptions(newSize, 0, [UIScreen mainScreen].scale);
//UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}