單獨某個界面橫屏顯示

AppDelegate中代碼

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (_allowRotation == 1) {       
           return UIInterfaceOrientationMaskAllButUpsideDown;    
     }else{        
           return (UIInterfaceOrientationMaskPortrait);    
     }
}
// 支持設(shè)備自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate{
    if (_allowRotation == 1) {
        return YES;
    }
    return NO;
}

在想要旋轉(zhuǎn)的ViewController中寫入

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = 1;

在ViewController消失的時候設(shè)置

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = 0;

屏幕旋轉(zhuǎn)監(jiān)聽方法

在特別的場景下,需要針對屏幕旋轉(zhuǎn)作特殊處理。在iOS系統(tǒng)下實現(xiàn)相關(guān)的功能還是比較方便的。

1.注冊UIApplicationDidChangeStatusBarOrientationNotification通知(舉例:在一個viewcontroller類的viewdidload中注冊該通知),示例代碼如下

注意這種方式監(jiān)聽的是StatusBar也就是狀態(tài)欄的方向,所以這個是跟你的布局有關(guān)的,你的布局轉(zhuǎn)了,才會接到這個通知,而不是設(shè)備旋轉(zhuǎn)的通知。當(dāng)我們關(guān)注的東西和布局相關(guān)而不是純粹設(shè)備旋轉(zhuǎn),我們使用上面的代碼作為實現(xiàn)方案比較適合。

 [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotificationobject:nil];

- (void)statusBarOrientationChange:(NSNotification *)notification

{


    UIInterfaceOrientation orientation = [[UIApplicationsharedApplication] statusBarOrientation];

    if (orientation == UIInterfaceOrientationLandscapeRight) // home鍵靠右

    {

        //

    }


    if (

        orientation ==UIInterfaceOrientationLandscapeLeft) // home鍵靠左

    {

        //

    }


    if (orientation == UIInterfaceOrientationPortrait)

    {

        //

    }

    if (orientation == UIInterfaceOrientationPortraitUpsideDown)

    {

        //

    }

}
2.注冊UIDeviceOrientationDidChangeNotification通知(舉例:我們同樣在一個viewcontroller類的viewdidload中注冊該通知),示例代碼如下:

注意到這種方式里面的方向還包括朝上或者朝下,很容易看出這個完全是根據(jù)設(shè)備自身的物理方向得來的,當(dāng)我們關(guān)注的只是物理朝向時,我們通常需要注冊該通知 來解決問題(另外還有一個加速計的api,可以實現(xiàn)類似的功能,該api較底層,在上面兩個方法能夠解決問題的情況下建議不要用,使用不當(dāng)性能損耗非常 大)。

  [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotificationobject:nil];

- (void)orientChange:(NSNotification *)noti

{


    NSDictionary* ntfDict = [noti userInfo];


    UIDeviceOrientation  orient = [UIDevicecurrentDevice].orientation;

    /*

     UIDeviceOrientationUnknown,

     UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom

     UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top

     UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right

     UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left

     UIDeviceOrientationFaceUp,              // Device oriented flat, face up

     UIDeviceOrientationFaceDown             // Device oriented flat, face down   */


           switch (orient)

        {

            caseUIDeviceOrientationPortrait:


                break;

            caseUIDeviceOrientationLandscapeLeft:



                break;

            caseUIDeviceOrientationPortraitUpsideDown:



                break;

            caseUIDeviceOrientationLandscapeRight:



                break;


            default:

                break;

        }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容