美文网首页
iOS使用UITabBarController时添加视图切换动画

iOS使用UITabBarController时添加视图切换动画

作者: 那一处风景ljz | 来源:发表于2018-07-17 11:09 被阅读27次

    1.创建UITabBarController

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
    //设置UITabBarController为根视图控制器
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UIViewController *VcA = [[ViewControllerA alloc] initWithNibName:@"VcA" bundle:nil];
        UIViewController *VcB = [[ViewControllerB alloc] initWithNibName:@"VcB" bundle:nil];
        self.tabBarController = [[UITabBarController alloc] init];
        self.tabBarController.viewControllers = @[VcA, VcB];
        self.tabBarController.delegate = self;  // 设置代理
        self.window.rootViewController = self.tabBarController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    2.在UITabBarController设置代理<UITabBarControllerDelegate>

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    {
        //获取选中的VC
        NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
        if (tabBarController.selectedIndex == shouldSelectIndex) {
            return YES;
        }
    
        CATransition *animation = [CATransition animation];
        animation.duration = 0.5;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.type = kCATransitionPush;//设置动画类型(可以试着换下看效果)
    
    //判断要选中的VC在已选中的左边还是右边,进行选择或左或右的push效果
        if (tabBarController.selectedIndex > shouldSelectIndex) {
            animation.subtype = kCATransitionFromLeft;
        } else {
            animation.subtype = kCATransitionFromRight;
        }
        // 下面这句可以换成[tabBarController.view.layer addAnimation:animation forKey:@"reveal"];  但是会导致tabBarController的整个标签栏也跟着一起有动画的效果,这样的效果并不是很好。
        [[[tabBarController valueForKey:@"_viewControllerTransitionView"] layer] addAnimation:animation forKey:@"animation"];
    
        return YES;
    }
    

    参考原文链接:https://www.jianshu.com/p/2525a93c8a68

    相关文章

      网友评论

          本文标题:iOS使用UITabBarController时添加视图切换动画

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