iOS ViewContrller轉場動畫學習筆記
本文只用來記錄學習的modal方式的轉場,一般需要實現的動畫包括:presentation,dismissal兩種可能實現的動畫.
FromView 和 ToView
在代碼中,經常使用FromView
和ToView
.FromView
表示當前的視圖view,ToView
表示要跳轉的視圖View.例如,presentation中,Avc present 出 Bvc,則A的view視圖就是FromView
,而B的view就是ToView
.相反,dismissal中,從Bvc dismiss 到Avc,此時Bvc的視圖view就是FromView
,A的視圖view就是ToView
.
Presenting和presented
這組概念也很重要,容易和上面的FromView
,ToView
混淆.如果是Avc present Bvc,那么A就是Presenting,B就是presented.不論當時的動作是Presentation還是Dismissal.UIKit中講A稱為presentingViewController
,B稱為presentedViewController
.通過主動或者被動很容易區分出來.
modalPrentationStyle
viewController中關于modal相關的屬性: FullScreen
和Custom
兩種.區別在與FullScreen
時候在containerView中會主動移除fromView
,而Custom
模式中,containerView不會主動移除fromView
.
這個屬性的特點非常重要.如果需要present方式時候想看到presentingViewController的view,那么這里一定要設置成
Custom
.
自定義轉場動畫
最簡單的轉場動畫需要如下步驟:
- 創建一個遵守
UIViewControllerAnimatedTransitioning
協議的對象 - 實現該協議的兩個重要的方法
//指定轉場動畫持續的時長
func transitionDuration(transitionContext: UIViewControllerContextTransitioning) -> NSTimeInterval
//轉場動畫的具體內容
func animateTransition(transitionContext: UIViewControllerContextTransitioning)
- 使得presentingViewController或者創建一個類遵守
UIViewControllerTransitioningDelegate
協議,并且實現如下兩個方法,返回一個前面創建的滿足Animation協議的對象
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
- 設置presentedViewController的transitioningDelegate屬性
上面步驟中,所有的動畫都在func animateTransition(transitionContext:)
中完成,這里需要發揮你的想象力.具體的FromView
到ToView
的動畫如何展現.
特殊的Modal轉場
這里直接引用的參考文章中的內容 -- 非常重要
Modal 轉場中需要做的事情和兩種容器 VC 的轉場一樣,但在細節上有些差異。UINavigationController 和 UITabBarController 這兩個容器 VC 的根視圖在屏幕上是不可見的(或者說是透明的),可見的只是內嵌在這兩者中的子 VC 中的視圖,轉場是從子 VC 的視圖轉換到另外一個子 VC 的視圖,其根視圖并未參與轉場;而 Modal 轉場,以 presentation 為例,是從 presentingView 轉換到 presentedView,根視圖 presentingView 也就是 fromView 參與了轉場。而且 NavigationController 和 TabBarController 轉場中的 containerView 也并非這兩者的根視圖。
Modal 轉場與兩種容器 VC 的轉場的另外一個不同是:Modal 轉場結束后 presentingView 可能依然可見,UIModalPresentationPageSheet 模式就是這樣。這種不同導致了 Modal 轉場和容器 VC 的轉場對 fromView 的處理差異:容器 VC 的轉場結束后 fromView 會被主動移出視圖結構,這是可預見的結果,我們也可以在轉場結束前手動移除;而 Modal 轉場中,presentation 結束后 presentingView(fromView) 并未主動被從視圖結構中移除。準確來說,是 UIModalPresentationCustom 這種模式下的 Modal 轉場結束時 fromView 并未從視圖結構中移除;UIModalPresentationFullScreen 模式的 Modal 轉場結束后 fromView 依然主動被從視圖結構中移除了。這種差異導致在處理 dismissal 轉場的時候很容易出現問題,沒有意識到這個不同點的話出錯時就會毫無頭緒。下面來看看 dismissal 轉場時的場景。
ContainerView 在轉場期間作為 fromView 和 toView 的父視圖。三種轉場過程中的 containerView 是 UIView 的私有子類,不過我們并不需要關心 containerView 具體是什么。在 dismissal 轉場中:
- UIModalPresentationFullScreen 模式:presentation 后,presentingView 被主動移出視圖結構,在 dismissal 中 presentingView 是 toView 的角色,其將會重新加入 containerView 中,實際上,我們不主動將其加入,UIKit 也會這么做,前面的兩種容器控制器的轉場里不是這樣處理的,不過這個差異基本沒什么影響。
- UIModalPresentationCustom 模式:轉場時 containerView 并不擔任 presentingView 的父視圖,后者由 UIKit 另行管理。在 presentation 后,fromView(presentingView) 未被移出視圖結構,在 dismissal 中,注意不要像其他轉場中那樣將 toView(presentingView) 加入 containerView 中,否則本來可見的 presentingView 將會被移除出自身所處的視圖結構消失不見。如果你在使用 Custom 模式時沒有注意到這點,就很容易掉進這個陷阱而很難察覺問題所在,這個問題曾困擾了我一天。
小結:建議是,不要干涉官方對 Modal 轉場的處理,我們去適應它。在 Custom 模式下,由于 presentingView 不受 containerView 管理,在 dismissal 轉場中不要像其他的轉場那樣將 toView(presentingView) 加入 containerView,否則 presentingView 將消失不見,而應用則也很可能假死;在 presentation 轉場中,切記不要手動將 fromView(presentingView) 移出其父視圖。
iOS 8 為協議添加了viewForKey:方法以方便獲取 fromView 和 toView,但是在 Modal 轉場里要注意,從上面可以知道,Custom 模式下,presentingView 并不受 containerView 管理,這時通過viewForKey:方法來獲取 presentingView 得到的是 nil,必須通過viewControllerForKey:得到 presentingVC 后來獲取。因此在 Modal 轉場中,較穩妥的方法是從 fromVC 和 toVC 中獲取 fromView 和 toView。
總結要點
- 一般來說,Modal 轉場的delegate由 presentedVC 提供
- 在presentation/push時候,要調用
containerView.addSubView(toView)
,toView加入到containerView是每個轉場必須完成的一步.modal中的Custom模式下的Dismiss 里不要將 toView添加到containerView,因為在present的時候,系統就沒有將它從containerView中刪除. - 在UIView.animation的completion中要調用如下代碼:
//2
let isCancelled = transitionContext.transitionWasCancelled()
transitionContext.completeTransition(!isCancelled)
- Modal 轉場一般需要 presentedVC 來提供轉場
UIViewControllerTransitioningDelegate
的代理. modal方式中自定義轉場時,決定轉場動畫效果的modalTransitionStyle屬性將被忽略.具體動畫由代理中返回的animationController決定
參考文獻
https://github.com/wazrx/XWTrasitionPractice
http://www.lxweimin.com/p/45434f73019e
http://www.cocoachina.com/ios/20160309/15605.html
偶然遇到的crash:
1 怎么連續dismiss兩個viewController?
直接[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];就可以了,控制器堆棧是dismiss掉下面的,上面的自動就dismiss了.或者直接將動畫設置成No2 連續push或者present會crash - 因為動畫的問題.如果有此類需求.直接關閉動畫