iOS 特定頁面橫屏退出或切換其他頁面再進(jìn)入仍保持橫屏

最近在研究播放器,播放器需要根據(jù)手機(jī)旋轉(zhuǎn)來切換橫豎屏狀態(tài),且橫豎屏都是同一個(gè)VC只是做了屏幕尺寸適配,全屏播放頁面有一個(gè)分享按鈕,參考其他APP要實(shí)現(xiàn),分享到微博微信等強(qiáng)制豎屏APP之后回來仍舊保持橫屏,而不是豎屏再切換回橫屏。如下圖所示。

IMG_0510.PNG
APP所有VC橫屏或豎屏

AppDelegate.m中有如下方法可以設(shè)置整個(gè)app的方向。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
      return UIInterfaceOrientationMaskLandscape;
}

該方法實(shí)質(zhì)上是制定window的方向,雖然一個(gè)app有多個(gè)VC,但是window都只有一個(gè),所以這個(gè)指定了window的方向,那所有VC的方向就都是統(tǒng)一的。

特定VC橫屏或豎屏

在iOS6之后,需要在某個(gè)頁面強(qiáng)制橫屏,可以使用以下代碼:

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationMaskLandscape);
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

此段代碼針對(duì)于在特定VC指定橫豎屏方向,但是由于我的demo中橫豎屏都是同一個(gè)VC,這個(gè)方法就不能采用了。

特定狀態(tài)橫屏且window橫屏

如果需要app進(jìn)入其他app再回來或退出后重新進(jìn)入都保持原來的方向,那么就需要改變window的方向,就需要用到APPDelegate中的supportedInterfaceOrientationsForWindow方法。只在特定狀態(tài)橫屏,那就需要一個(gè)標(biāo)識(shí)符來判定當(dāng)前是否需要橫屏。代碼如下:

//AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic, assign) BOOL isLandscape;
@end

//AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (_ isLandscape) {
            return UIInterfaceOrientationMaskLandscape;
    }
    else
    {
           return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

//Player.m
- (void)backToPortrait//回到豎屏
{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = NO;
}

- (void)enterToLandscape//進(jìn)入橫屏
{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = YES;
}

但是發(fā)現(xiàn)加入以上代碼,導(dǎo)致全屏模式下無法通過旋轉(zhuǎn)手機(jī)方向進(jìn)入回到豎屏模式了,看了一下是因?yàn)閕sLandscape為YES,所以始終return UIInterfaceOrientationMaskLandscape。最終修改AppDelegate.m中代碼如下:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (_isLandscape) {
        UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
            return UIInterfaceOrientationMaskLandscape;
        }else { // 橫屏后旋轉(zhuǎn)屏幕變?yōu)樨Q屏
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    else
    {
           return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

此處需要注意,判斷屏幕方向只能使用UIDeviceOrientation手機(jī)設(shè)備方向而不能用UIInterfaceOrientation狀態(tài)欄方向,因?yàn)闄M屏狀態(tài)下,走isLandscape邏輯,不改變當(dāng)前VC方向,所以雖然手機(jī)方向改變,但狀態(tài)欄方向不變,用狀態(tài)方向判斷會(huì)導(dǎo)致屏幕無法旋轉(zhuǎn)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容