美文网首页
UITabBarController 底部点击Tabbar带缩放

UITabBarController 底部点击Tabbar带缩放

作者: 白色天空729 | 来源:发表于2018-08-16 17:01 被阅读21次
Untitled.gif
#import "MainTabbarVC.h"
#import "ViewController.h"
#import "SecViewController.h"

@interface MainTabbarVC ()<UITabBarControllerDelegate>

@end

@implementation MainTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.delegate = self;
    ViewController *vc = [ViewController new];
    SecViewController *vc2 = [SecViewController new];
    [self addChildViewControllerWithTitle:@"首页" andImage:[UIImage imageNamed:@"mainOne"] andSelectedImage:[UIImage imageNamed:@"mainOne_select"] andClass:vc];
    [self addChildViewControllerWithTitle:@"首页2" andImage:[UIImage imageNamed:@"mainOne"] andSelectedImage:[UIImage imageNamed:@"mainOne_select"] andClass:vc2];
}

-(void)addChildViewControllerWithTitle:(NSString *)title andImage:(UIImage *)image andSelectedImage:(UIImage *)selectedImage andClass:(UIViewController *)controller
{
    controller.tabBarItem.title = title;
    controller.tabBarItem.image = image;
    controller.tabBarItem.selectedImage = selectedImage;
    
    [[UITabBarItem appearance] setTitleTextAttributes:
  @{NSForegroundColorAttributeName:[[UIColor blackColor]colorWithAlphaComponent:0.8],NSFontAttributeName:[UIFont systemFontOfSize:11.5f]} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor],NSFontAttributeName:[UIFont systemFontOfSize:11.5f]} forState:UIControlStateSelected];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:controller];
    [self addChildViewController:nav];
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= NO;
    pulse.fromValue= [NSNumber numberWithFloat:0.3];
    pulse.toValue= [NSNumber numberWithFloat:1.0];
    [[tabbarbuttonArray[self.selectedIndex] layer]
     addAnimation:pulse forKey:nil];
}

@end

关键代码:

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation *pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= NO;
    pulse.fromValue= [NSNumber numberWithFloat:0.3];
    pulse.toValue= [NSNumber numberWithFloat:1.0];
    [[tabbarbuttonArray[self.selectedIndex] layer]
     addAnimation:pulse forKey:nil];
}

相关文章

网友评论

      本文标题:UITabBarController 底部点击Tabbar带缩放

      本文链接:https://www.haomeiwen.com/subject/ppyobftx.html