实现tabbar点击的帧动画效果, 可以用下面两种方法
Jietu20190722-103120-HD.gif
方法1:
1.在UITabBarController 里面
@interface UITabBarController () <UITabBarControllerDelegate>
//注意数组是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.实现 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]);
}
}
//获取当前的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 , 和方法一区别不大, 只需修改下面代码
//注意数组是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]);
}
}
//获取当前的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.另外你可以设置tabbar的其他属性
//1设置线条颜色(遮挡法)
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标题颜色
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;
}