VCTransitionsLibrary 提供了许多适用于入栈,出栈,模态等场景下控制器切换时的转场动画.它本身提供了一个定义好的转场动画库,你可以拖到自己工程中直接使用;也提供了许多拥有不同转场动画效果”互动控制器”,你可以直接使用这些控制器来和自定义动画效果配合使用;而不是自己控制去控制交互.
项目主页: VCTransitionsLibrary
最新示例: 点击下载
注意: 自定义视图控制器的转场动画为iOS7 + 通过 UIViewControllerTransitioningDelegate协议, UINavigationControllerDelegate协议和 UITabBarControllerDelegate 协议提供的系统级别的支持.这个库的意义在于定义了常用的动画效果,并封装了常用的交互操作,简化了iOS交互式转场动画的编码量!
pod "VCTransitionsLibrary"
把文件 AnimationControllers
和 InteractionControllers 文件夹下所有代码复制
到工程中即可.
在自定义转场动画时,有两类关键的类:
注意: 动画和交互是完全独立的,这意味着你可以在其他任何自定义控制器上独立使用交互控制器-很酷!
AnimationControllers 文件夹中提供了许多可以整合进你的工程中的动画控制器:
UIViewControllerTransitioningDelegate 协议被用来在模态控制器显示/隐藏时提供一个动画控制器.当一个视图控制器被模态显示或隐藏时,它的transitioningDelegate属性用来提供UIViewControllerTransitioningDelegate协议的支持.担当代理角色的类,通过 animationControllerForPresentedController: presentingController: sourceController: 方法返回模态显示时的动画, 通过 animationControllerForDismissedController: 返回模态消失时的动画即可.
UINavigationController 有一个
id<UINavigationControllerDelegate> delegate 属性.只需要让它的代理通过 navigationController: animationControllerForOperation: fromViewController: toViewController: 返回某个动画效果即可.
为了同时设置出栈/入栈都合适的动画效果(或者说,出栈/入栈时能使用相反方向的动画),你可以参考下面代码:
- (id<UIViewControllerAnimatedTransitioning>)navigationController:
(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC {
// 出栈时,要反转动画方向.
_animationController.reverse = operation == UINavigationControllerOperationPop;
return _animationController;
}
UITabBarController 有一个 id<UITabBarControllerDelegate> delegate属性,只需要让它的代理通过tabBarController: animationControllerForTransitionFromViewController: toViewController:返回某个动画效果即可.
为了给动画一个合适的方向,你可以比较两个视图控制器的索引:
- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { NSUInteger fromVCIndex = [tabBarController.viewControllers indexOfObject:fromVC]; NSUInteger toVCIndex = [tabBarController.viewControllers indexOfObject:toVC]; _animationController.reverse = fromVCIndex < toVCIndex; return _animationController; }
交互控制器和动画控制器配合使用,可以实现交互式的动画转场效果,比如可以让用户通过手势来控制页面间的导航.交互控制器允许用户在一个转场动画中前进,后退,甚至退出.
交互控制器负责给视图添加手势,并负责在用户使用某个手势时进行相应地导航操作.
UIViewControllerTransitioningDelegate 协议,也用来提供对交互式转场的支持.下面是一个结合清扫手势和翻页动画的例子:
//实例变量,通常在你的初始化方法初始化它们 CEFlipAnimationController *_animationController; CESwipeInteractionController *_interactionController; - (id<UIViewControllerAnimatedTransitioning>) animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source { // 允许交互控制器绑定它的手势识别器. [_interactionController wireToViewController:presented forOperation:CEInteractionOperationDismiss]; _animationController.reverse = NO; return _animationController; } - (id<UIViewControllerAnimatedTransitioning>) animationControllerForDismissedController:(UIViewController *)dismissed { _animationController.reverse = YES; return _animationController; } - (id<UIViewControllerInteractiveTransitioning>) interactionControllerForDismissal: (id<UIViewControllerAnimatedTransitioning>)animator { // 如果有交互控制器被触发了,就直接使用它.返回nil,是为了支持用户通过点击某个按钮直接返回;此时不会触发交互控制器. return _interactionController.interactionInProgress ? _interactionController : nil; }
UINavigationControllerDelegate 也有方法为交互式转场提供支持.一个典型的类似于上上面代码的模式:
// 实例变量,通常在你的初始化方法中初始化它们. CEFlipAnimationController *_animationController; CESwipeInteractionController *_interactionController; - (id<UIViewControllerAnimatedTransitioning>) navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { // 把交互控制器绑定到你的视图控制器上. [_interactionController wireToViewController:toVC forOperation:CEInteractionOperationPop]; _animationController.reverse = operation == UINavigationControllerOperationPop; return _animationController; } - (id <UIViewControllerInteractiveTransitioning>) navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>)animationController { //如果有交互控制器被触发了,就直接使用它.返回nil,是为了支持用户通过点击某个按钮直接返回;此时不会触发交互控制器. return _interactionController.interactionInProgress ? _interactionController : nil; }
UITabBarControllerDelegate 协议也为交互式转场提供了支持.但是由于代理方法在首次初始化时不被执行,所有需要其他方式来绑定交互控制器,如KVO:
@implementation TabBarViewController { CEFoldAnimationController *_animationController; CESwipeInteractionController *_swipeInteractionController; } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { self.delegate = self; // 创建交互/动画控制器. _swipeInteractionController = [CESwipeInteractionController new]; _animationController = [CEFoldAnimationController new]; _animationController.folds = 3; // 使用观察者模式监测被选中的选择器的变化情况. [self addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:nil]; } return self; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"selectedViewController"] ) { // 把交互控制器绑定到视图控制器上. [_swipeInteractionController wireToViewController:self.selectedViewController forOperation:CEInteractionOperationTab]; } } - (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { NSUInteger fromVCIndex = [tabBarController.viewControllers indexOfObject:fromVC]; NSUInteger toVCIndex = [tabBarController.viewControllers indexOfObject:toVC]; _animationController.reverse = fromVCIndex < toVCIndex; return _animationController; } -(id<UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController { return _swipeInteractionController.interactionInProgress ? _swipeInteractionController : nil; } @end