最近在做橫豎屏切換的時(shí)候,發(fā)現(xiàn)一個(gè)crash,是一個(gè)平時(shí)沒有注意到的點(diǎn)志衍。
UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation 'landscapeLeft' must match a supported interface orientation: 'portrait'!
從描述上看,錯(cuò)誤很明顯聊替,我們從方法的描述上看看。
Returns the interface orientation to use when presenting the view controller.
The system calls this method when presenting the view controller full screen. When your view controller supports two or more orientations but the content appears best in one of those orientations, override this method and return the preferred orientation.
If your view controller implements this method, your view controller’s view is shown in the preferred orientation (although it can later be rotated to another supported rotation). If you do not implement this method, the system presents the view controller using the current orientation of the status bar.
那么如果supportedInterfaceOrientations
和preferredInterfaceOrientationForPresentation
返回的內(nèi)容不符培廓,是不是就會(huì)crash惹悄,經(jīng)過試驗(yàn)的確會(huì)crash。
但是我們?cè)?code>dismiss的時(shí)候呢肩钠,經(jīng)過試驗(yàn)表明泣港,也是會(huì)crash的,特別是preferredInterfaceOrientationForPresentation
返回的是UIApplication.shared.statusBarOrientation
時(shí)价匠,是一個(gè)非常容易被忽略的問題当纱。
那么push呢?表現(xiàn)是怎么樣的踩窖?
經(jīng)過試驗(yàn)坡氯,在pop的時(shí)候,如果橫豎屏狀態(tài)不一致,并不會(huì)crash箫柳,但是轉(zhuǎn)場動(dòng)畫采用了系統(tǒng)默認(rèn)的了手形,不會(huì)進(jìn)入轉(zhuǎn)場動(dòng)畫的delegate。
所以悯恍,如果需要做流暢的轉(zhuǎn)場以及橫豎屏切換库糠,在一些場合下,還是需要使用present方式涮毫,并且需要在雙方都確保supportedInterfaceOrientations
和preferredInterfaceOrientationForPresentation
的正確瞬欧。
自定義動(dòng)畫
在做自定義轉(zhuǎn)場動(dòng)畫的時(shí)候,可以注意到罢防,如果是從豎屏進(jìn)入橫屏的動(dòng)畫過程中艘虎,橫豎屏的狀態(tài)是正確的,但是橫豎屏狀態(tài)只能存在一個(gè)篙梢,也就是[UIDevice currentDevice].orientation
顷帖,那么系統(tǒng)是怎么做到的呢。
重寫UIViewControllerAnimatedTransitioning
中的動(dòng)畫方案- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
渤滞,在此時(shí)獲取屏幕方向[UIApplication sharedApplication].statusBarOrientation
贬墩,發(fā)現(xiàn)已經(jīng)轉(zhuǎn)換到下一個(gè)controller的方向了,那么前一個(gè)controller為什么顯示正常呢妄呕。我們看一下前一個(gè)fromController.view.transform
陶舞,發(fā)現(xiàn)他并不是CGAffineTransformIdentity
,這樣就一目了然了绪励。系統(tǒng)在屏幕轉(zhuǎn)向變化的時(shí)候肿孵,會(huì)把上一個(gè)視圖自動(dòng)旋轉(zhuǎn)90度,這樣就可以無縫的轉(zhuǎn)場動(dòng)畫疏魏。
那么另一個(gè)問題是停做,此時(shí)的controller的生命周期會(huì)是怎么樣的。經(jīng)過嘗試大莫,此時(shí)已經(jīng)在viewWillAppear:
和viewWillDisappear
之后了蛉腌,符合我們的預(yù)期。