您的位置:首页 > 移动开发 > IOS开发

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

2017-03-10 16:08 661 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: