iOS7新特性 ViewController轉場切換(一) 以前總結和關鍵API介紹
@在iOS7之前,View Controller的切換主要有4種:
1. Push/Pop,NavigationViewController
2. Present and dismis Modal
3. UITabBarController
4. addChildViewController(一般用於自訂的繼承於 UIViewController 的容器子類)
iOS5,調用- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
(1)前面3種方法這裡就不多說了,很常見的系統方法.至於第四種,我在前面文章-剖析網易標籤欄的效果中已經做了闡述,但是它提供的容器轉場動畫只可以實現一些簡單的UIView動畫,但是難以重用,耦合高.
(2)關鍵的API:
A.動畫控制器 (Animation Controllers) 遵從 UIViewControllerAnimatedTransitioning 協議,並且負責實際執行動畫。
B.互動控制器 (Interaction Controllers) 通過遵從 UIViewControllerInteractiveTransitioning 協議來控制可互動轉場。
C.轉場代理 (Transitioning Delegates) 根據不同的轉場類型方便的提供需要的動畫控制器和互動控制器。
其中UINavigationControllerDelegate delegate 中新增了2個方法給NavigationController
UIViewControllerTransitioningDelegate 新增transitioningDelegate 給Modal的Present和Dismis
UITabBarControllerDelegate delegate
- (id )tabBarController:(UITabBarController *)tabBarController
interactionControllerForAnimationController: (id )animationController NS_AVAILABLE_IOS(7_0);
- (id )tabBarController:(UITabBarController *)tabBarController
animationControllerForTransitionFromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC NS_AVAILABLE_IOS(7_0);
D.轉場上下文 (Transitioning Contexts) 定義了轉場時需要的中繼資料,比如在轉場過程中所參與的視圖控制器和視圖的相關屬性。 轉場內容物件遵從 UIViewControllerContextTransitioning 協議,並且這是由系統負責產生和提供的。
E.轉場協調器(Transition Coordinators) 可以在運行轉場動畫時,並行的運行其他動畫。 轉場協調器遵從 UIViewControllerTransitionCoordinator 協議。
(3)新的API主要提供了2種VC切換的方式:
A.非互動式切換,即定義一種從一個VC到另一個VC的動畫效果,切換的時候自動播放,
B.互動式切換,這種方式同樣需要定義動畫效果,只是這個動畫效果會根據跟隨互動式手勢來切換VC並同時播放動畫效果。iOS7提供了一個預設的基於百分比的動畫實現 UIPercentDrivenInteractiveTransition,而且根據WWDC的說明,最簡單的實現互動式動畫的方法就是通過繼承 UIPercentDrivenInteractiveTransition。