關(guān)于設(shè)置橫豎屏的問題總結(jié)

項(xiàng)目需求:首頁不橫屏 從首頁進(jìn)入下一個(gè)控制器是一個(gè)播放界面,用于播放視頻,該界面會(huì)自動(dòng)旋轉(zhuǎn)屏幕,并且可以手動(dòng)旋轉(zhuǎn)屏幕,可以鎖定屏幕。

程序結(jié)構(gòu):根控制器 UINavigationController , 首頁控制器 UIViewControlle 播放控制器 UIViewController

下面我們來實(shí)現(xiàn)屏幕旋轉(zhuǎn)
  1. 首先,我們需要一個(gè)全局變量,來標(biāo)示是否允許旋轉(zhuǎn),可以在AppDelegate.h文件中 如下聲明
    @property (nonatomic,assign)BOOL allowRotation;
    這里,我們可以定義一個(gè)宏,方便我們使用AppDelegate
    #define ApplicationDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

然后在AppDelegate.m文件中 實(shí)現(xiàn)允許屏幕的方向函數(shù) 如下

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

2.然后在根控制器中實(shí)現(xiàn)允許屏幕旋轉(zhuǎn)的方法 如下
<pre>- (BOOL)shouldAutorotate{
return ApplicationDelegate.allowRotation;
}
</pre>
3.然后就可以愉快的玩耍了

當(dāng)想要旋轉(zhuǎn)屏幕的時(shí)候,就去修改ApplicationDelegate.allowRotation的值,默認(rèn)是NO,是不支持旋轉(zhuǎn)的,所以滿足我們的需求

當(dāng)我們從首頁進(jìn)入播放界面的時(shí)候
在頁面將要出現(xiàn)的時(shí)候 設(shè)置為YES
在頁面將要消失的時(shí)候 設(shè)置為NO
如下:

-  (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    ApplicationDelegate.allowRotation==NO;
}

-  (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    ApplicationDelegate.allowRotation==YES;
}

在控制器下還需添加一個(gè)通知,用來監(jiān)聽手機(jī)方向是否改變,如下:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeFrames:) name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

當(dāng)方向改變的時(shí)候,會(huì)觸發(fā)changeFrames方法,然后在該方法里面去判斷ApplicationDelegate.allowRotation的值,如果YES就去改變控件的Frame

4.手動(dòng)旋轉(zhuǎn)屏幕

有時(shí)候自動(dòng)旋轉(zhuǎn)并不能滿足我們的需求,還需要用戶手動(dòng)的去點(diǎn)擊按鈕,觸發(fā)旋轉(zhuǎn)事件,這里,提供手動(dòng)旋轉(zhuǎn)函數(shù),直接調(diào)用即可。

/**
 *  手動(dòng)旋轉(zhuǎn)屏幕方法
 *
 *  @param orientation 屏幕方向
 */
- (void)forceOrientation: (UIInterfaceOrientation)orientation {
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@""setOrientation:"");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget: [UIDevice currentDevice]];
        int val = orientation;
        [invocation setArgument:&amp;val atIndex:2];
        [invocation invoke];
    }
}

注意:屏幕旋轉(zhuǎn)的前提是ApplicationDelegate.allowRotation的值為YES才可以,否則會(huì)無效或者屏幕出現(xià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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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