VCTransitionsLibrary –自訂iOS互動式轉場動畫的庫,ios轉場動畫
簡介
VCTransitionsLibrary 提供了許多適用於入棧,出棧,模態等情境下控制器切換時的轉場動畫.它本身提供了一個定義好的轉場動畫庫,你可以拖到自己工程中直接使用;也提供了許多擁有不同轉場動畫效果”互動控制器”,你可以直接使用這些控制器來和自訂動畫效果配合使用;而不是自己控制去控制互動.
項目首頁: VCTransitionsLibrary
最新樣本: 點擊下載
注意: 自訂視圖控制器的轉場動畫為iOS7 + 通過 UIViewControllerTransitioningDelegate協議, UINavigationControllerDelegate協議和 UITabBarControllerDelegate 協議提供的系統層級的支援.這個庫的意義在於定義了常用的動畫效果,並封裝了常用的互動操作,簡化了iOS互動式轉場動畫的編碼量!
快速入門運行環境
安裝使用 CocoaPods 安裝
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