VCTransitionsLibrary 提供了许多适用于入栈,出栈,模态等场景下控制器切换时的转场动画.它自己提供了一个定义好的转场动画库,你能够拖到本身工程中直接使用;也提供了许多拥有不一样转场动画效果”互动控制器”,你能够直接使用这些控制器来和自定义动画效果配合使用;而不是本身控制去控制交互.ios
项目主页: VCTransitionsLibrarygit
最新示例: 点击下载github
注意: 自定义视图控制器的转场动画为iOS7 + 经过 UIViewControllerTransitioningDelegate协议, UINavigationControllerDelegate协议和 UITabBarControllerDelegate 协议提供的系统级别的支持.这个库的意义在于定义了经常使用的动画效果,并封装了经常使用的交互操做,简化了iOS交互式转场动画的编码量!框架
iOS 7+动画
ARC编码
pod "VCTransitionsLibrary"
把文件 AnimationControllers
和 InteractionControllers 文件夹下全部代码复制
到工程中便可.spa
在自定义转场动画时,有两类关键的类:代理
动画控制器 – 这个类是用来实现自定义动画的.但你声明想要使用自定义动画时,你应该提供一个动画控制器.这个类会实现须要的动画,完成时会通知框架.code
交互控制器 – 这个类是用来管理交互的-那些一般由某个手势空控制的交互,容许用户经过滑动,轻扫或执行其余操做来实现两个视图控制器的导航.必须指出的是,交互控制器容许导航取消,例如,一个用户能够在正在导航至某一页面时,忽然改变主意,而后取消了操做.orm
注意: 动画和交互是彻底独立的,这意味着你能够在其余任何自定义控制器上独立使用交互控制器-很酷!
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