国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS 橫豎屏切換

2019-11-09 17:01:06
字體:
來源:轉載
供稿:網友

一、基本說明

UIDeviceOrientation      是機器硬件的當前旋轉方向   這個你只能取值 不能設置

UIInterfaceOrientation   是你程序界面的當前旋轉方向   這個可以設置

Portrait 表示 縱向,Landscape 表示 橫向。

typedef enum {      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  } UIDeviceOrientation;  

typedef enum {      UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,      UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,      UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft    //注意Interface和Device的左右是相反的} UIInterfaceOrientation;  

#define UIDeviceOrientationIsPortrait(orientation)  ((orientation) == UIDeviceOrientationPortrait || (orientation) == UIDeviceOrientationPortraitUpsideDown)  #define UIDeviceOrientationIsLandscape(orientation) ((orientation) == UIDeviceOrientationLandscapeLeft || (orientation) == UIDeviceOrientationLandscapeRight) 

判斷設備現在的方向:

C代碼  收藏代碼- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {      //宣告一個UIDevice指標,並取得目前Device的狀況      UIDevice *device = [UIDevice currentDevice] ;             //取得當前Device的方向,來當作判斷敘述。(Device的方向型態為Integer)      switch (device.orientation) {          case UIDeviceOrientationFaceUp:          NSLog(@"螢幕朝上平躺");              break;                        case UIDeviceOrientationFaceDown:          NSLog(@"螢幕朝下平躺");              break;                        //系統無法判斷目前Device的方向,有可能是斜置           case UIDeviceOrientationUnknown:          NSLog(@"未知方向");              break;                        case UIDeviceOrientationLandscapeLeft:          NSLog(@"螢幕向左橫置");              break;                        case UIDeviceOrientationLandscapeRight:          NSLog(@"螢幕向右橫置");              break;                        case UIDeviceOrientationPortrait:          NSLog(@"螢幕直立");              break;                        case UIDeviceOrientationPortraitUpsideDown:          NSLog(@"螢幕直立,上下顛倒");              break;                        default:          NSLog(@"無法辨識");              break;      }        // Return YES for supported orientations      return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左橫向, YES 表示支持所有方向  }   

或者

C代碼  收藏代碼- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {      UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;      if (UIDeviceOrientationIsLandscape(deviceOrientation)) NSLog(@"橫向");          else if(UIDeviceOrientationIsPortrait(deviceOrientation)) NSLog(@"縱向");            // // Return YES for supported orientations      return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左橫向, YES 表示支持所有方向  }  

二、使用場景

1、一般情形,所有界面都支持橫豎屏切換

如果App的所有切面都要支持橫豎屏的切換,那只需要勾選【General】 中的【Device Orientation】,選擇希望支持的方向即可。

blob.png圖中支持豎屏和Home在右側

如上設置完之后,當設備豎屏的時候,所有的界面都是豎屏顯示的;而當設備橫屏Home在右側時,所有的界面會橫屏顯示。其他方向不支持,界面不會改變。

這里有個坑:在iOS 9 之后橫屏時,狀態欄會消失。解決方法:確保plist 中的【View controller-based status bar appearance】為YES,然后重寫ViewController的 - (BOOL)PRefersstatusBarHidden ,返回值是NO。

- (BOOL)prefersStatusBarHidden{    return NO;}2、特殊情形,個別界面固定方向,其他所有界面都支持橫豎屏切換

這種情況,在【General】-->【Device Orientation】中設置好支持的方向后,只需要在這些特殊的固定方向的視圖控制器中重寫兩個方法:

// 支持設備自動旋轉- (BOOL)shouldAutorotate{    return YES;}/** *  設置特殊的界面支持的方向,這里特殊界面只支持Home在左側的情況*/- (UIInterfaceOrientationMask)supportedInterfaceOrientations {    return UIInterfaceOrientationMaskLandscapeRight;}
3、特殊情形:個別界面支持橫豎屏切換,其他所有界面都固定方向

可能大多數App會是這種需求,某些特殊界面只能橫屏,如視頻播放類App。這里有兩種處理方式:方式一在【General】-->【Device Orientation】中設置好需要支持的所有方向。然后使用一個基類控制器,在基類控制器中重寫兩個控制橫豎屏的方法:

// 支持設備自動旋轉- (BOOL)shouldAutorotate{    return YES;}// 支持豎屏顯示- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait;}

再然后,特殊的界面上再重寫這倆方法,讓其可以自動切換方向。

// 如果需要橫屏的時候,一定要重寫這個方法并返回NO

- (BOOL)prefersStatusBarHidden{    return NO;}// 支持設備自動旋轉- (BOOL)shouldAutorotate{    return YES;}// 支持橫屏顯示- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    // 如果該界面需要支持橫豎屏切換    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;    // 如果該界面僅支持橫屏   // return UIInterfaceOrientationMaskLandscapeRight;}

方式二用方式一的方法,還需要借助一個基類,所有的控制器都要繼承這個基類,太麻煩?另一種方式,是借助通知來控制界面的橫豎屏切換。還是整個App中大部分界面都是豎屏,某個界面可以橫豎屏切換的情況。

首先,在【General】-->【Device Orientation】設置僅支持豎屏,like this:

blob.pngDevice Orientation

然后在特殊的視圖控制器里的ViewDidLoad中注冊通知:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];

通知方法的實現過程:

- (void)deviceOrientationDidChange{    NSLog(@"deviceOrientationDidChange:%ld",(long)[UIDevice currentDevice].orientation);    if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) {        [[UIapplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];        [self orientationChange:NO];        //注意: UIDeviceOrientationLandscapeLeft 與 UIInterfaceOrientationLandscapeRight    } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {        [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];        [self orientationChange:YES];    }}- (void)orientationChange:(BOOL)landscapeRight{    if (landscapeRight) {        [UIView animateWithDuration:0.2f animations:^{            self.view.transform = CGAffineTransformMakeRotation(M_PI_2);            self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);        }];    } else {        [UIView animateWithDuration:0.2f animations:^{            self.view.transform = CGAffineTransformMakeRotation(0);            self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);        }];    }}// 用到的兩個宏:    #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)    #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

最重要的一點:需要重寫如下方法,并且返回NO。

- (BOOL)shouldAutorotate{    return NO;}

這樣,在設備出于橫屏時,界面就會變成橫屏,設備處于豎屏時,界面就會變成豎屏。

填坑

上面方式二,因為【General】-->【Device Orientation】因為只設置了豎屏,所以當橫屏時,如果有鍵盤彈出,鍵盤是豎屏時的樣式。解決辦法:在【General】-->【Device Orientation】中加上橫屏時的方向。

如果VieController 是放在UINavigationController或者UITabBarController中,需要重寫它們的方向控制方法。

// UINavigationController:- (BOOL)shouldAutorotate{    return [self.topViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    return [self.topViewController supportedInterfaceOrientations];}// UITabBarController:- (BOOL)shouldAutorotate{    return [self.selectedViewController shouldAutorotate];}- (UIInterfaceOrientationMask)supportedInterfaceOrientations{    return [self.selectedViewController supportedInterfaceOrientations];}

如果想要點擊某個按鈕之后,強制將豎屏顯示的界面變成橫屏呢?有人可能會想到這樣寫:

// 橫屏- (IBAction)landscapAction:(id)sender {    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];    [self orientationChange:YES];}

但是按照上面的寫法,會導致返回到之前的界面時,視圖方向錯誤,即使返回前執行如下代碼:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];[self orientationChange:NO];

也沒有作用,下面是在開源工程中無意看到的寫法:

// 橫屏- (IBAction)landscapAction:(id)sender {    [self interfaceOrientation:UIInterfaceOrientationLandscapeRight];}// 豎屏- (IBAction)portraitAction:(id)sender {    [self interfaceOrientation:UIInterfaceOrientationPortrait];}- (void)interfaceOrientation:(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:&val atIndex:2];        [invocation invoke];    }}

上面的方法會將設備的方向強制設置為某個方向,然后再監控設備方向改變的通知,即可實現橫豎屏切換。

參考:
http://justcoding.iteye.com/blog/1472932
http://www.cocoachina.com/ios/20160722/17148.html

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 炎陵县| 花垣县| 沙湾县| 虞城县| 长白| 宜宾市| 措美县| 孟津县| 靖安县| 隆林| 淮北市| 正阳县| 崇州市| 筠连县| 本溪市| 上栗县| 六安市| 兴国县| 五台县| 湖口县| 吉木萨尔县| 卢龙县| 岳池县| 宜兰县| 兰考县| 克什克腾旗| 灵璧县| 贞丰县| 句容市| 从化市| 开化县| 潞西市| 承德县| 泗阳县| 隆化县| 宁武县| 庆元县| 济源市| 民县| 青铜峡市| 阿鲁科尔沁旗|