iOS 在TabBarController视图切换的时候添加动画

24 篇文章 0 订阅

iOS中TabBarController是很强大的控制器,使用起来也非常的方便,但是它点击tabbaritem切换视图时却不像navigation一样有种划入划出的效果,那么怎么在TabBarController控制器切换的时候添加动画呢?
在TabBarControllerDelegate中已经定义好了方法,我们可以在自己的TabBarController中实现这个协议中的方法,就可以在切换视图的时候加入动画效果。
TabBarControllerDelegate中有两个方法,这里先讲第一个
github地址:https://github.com/ColdChains/LAXTabBarControllerAnimation

一、创建一个类继承NSObject,实现协议UIViewControllerAnimatedTransitioning

#import "AnimationManager.h"

@interface AnimationManager ()
@end

@implementation AnimationManager

static UIView *presentingView;

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {

    return 1.0;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

    // 获取fromVc和toVc
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *fromView = [[UIView alloc] init];;
    UIView *toView = [[UIView alloc] init];

    if ([transitionContext respondsToSelector:@selector(viewForKey:)]) {
        // fromVc 的view
        fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
        // toVc的view
        toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    }else {
        // fromVc 的view
        fromView = fromVc.view;
        // toVc的view
        toView =toVc.view;
    }

    CGFloat x = [UIScreen mainScreen].bounds.size.width;
    if ([toVc.title isEqualToString:@"B"]) {
        x = -x;//根据title判断是从哪个控制器跳转到哪个控制器 设置动画的方向
    }

    // 转场环境
    UIView *containView = [transitionContext containerView];
    toView.frame = CGRectMake(-x, 0, containView.frame.size.width, containView.frame.size.height);

    [containView addSubview:fromView];
    [containView addSubview:toView];

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromView.transform = CGAffineTransformTranslate(fromView.transform, x, 0);
        toView.transform = CGAffineTransformTranslate(toView.transform, x, 0);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];

    }];

}
@end

创建TabBarController,设置代理,并实现UITabBarControllerDelegate的代理方法。
在animationControllerForTransitionFromViewController方法中返回刚才写的类的实例。

#import "DMMainViewController.h"
#import "AnimationManager.h"

@interface DMMainViewController ()<UITabBarControllerDelegate>

@end

@implementation DMMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    [self setChildchildViewController:[[UITableViewController alloc] init] title:@"A"];
    [self setChildchildViewController:[[UITableViewController alloc] init] title:@"B"];
//    [self setChildchildViewController:[[UITableViewController alloc] init] title:@"C"];
//    [self setChildchildViewController:[[UITableViewController alloc] init] title:@"D"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setChildchildViewController:(UIViewController *)viewController title:(NSString *)title {

    viewController.title = title;

    UINavigationController *naviVc = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self addChildViewController:naviVc];

}

// 实现协议
- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    return [[AnimationManager alloc] init];
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值