屏幕旋转调用的方法

来源:互联网 发布:淘宝开店流程步骤ppt 编辑:程序博客网 时间:2024/05/18 22:50

UIDeviceOrientation      是机器硬件的当前旋转方向   这个你只能取值 不能设置

UIInterfaceOrientation   是你程序界面的当前旋转方向   这个可以设置



下面这两个方法只有在控制器中才能使用,如果在一般的view中使用layoutsubviews;

1.//将要旋转到某个方向的时候调用此方法(iOS8之前)

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

    //Landscape : 横屏  Portrait: 竖屏

    

    // 传入一个方向值如果是横屏则返回YES

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {

        NSLog(@"横屏");

    }

    

    // 传入一个方向值如果是竖屏则返回YES

    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {

        NSLog(@"竖屏");

    }

}


2.#pragma mark iOS8以后的方法

// 如果实现了iOS8以后的方法则旧版方法会覆盖


//视图发生了大小改变的时候会调用此方法   大小改变 == 横竖切换

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

{

    NSLog(@"size; %@"NSStringFromCGSize(size));

    

    // 建议如果背景色不一致的情况下做动画同步的处理否则视觉上很不舒服

    

    //动画同步 --> 系统默认0.25 而选择是0.4

    [UIView animateWithDuration:[coordinator transitionDurationanimations:^{

        //方法一:

        if (size.width == HMScreenMaxWidth1024) {

            NSLog(@"横屏");

            self.dockView.width = HMDockMaxWidth;

            self.dockView.height = HMScreenMinWidth768;

        } else {

            NSLog(@"竖屏");

            self.dockView.width = HMDockMinWidth;

            self.dockView.height = HMScreenMaxWidth1024;

        }


    }];

    

    

    

    //方法二

//    if (size.width > size.height) {

//        NSLog(@"横屏");

//    }

    

    //方法三 orientation: 方向

//    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {

//        NSLog(@"横屏");

//    }

}


4.

AppDelegate下面的屏幕旋转方法

#pragma mark - 屏幕旋转的//- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window//{//    return UIInterfaceOrientationMaskPortrait;//返回竖屏//}



5. iOS6 使用的旋转方法

  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     //宣告一個UIDevice指標,並取得目前Device的狀況  
  4.     UIDevice *device = [UIDevice currentDevice] ;   
  5.       
  6.     //取得當前Device的方向,來當作判斷敘述。(Device的方向型態為Integer)  
  7.     switch (device.orientation) {  
  8.         case UIDeviceOrientationFaceUp:  
  9.         NSLog(@"螢幕朝上平躺");  
  10.             break;  
  11.               
  12.         case UIDeviceOrientationFaceDown:  
  13.         NSLog(@"螢幕朝下平躺");  
  14.             break;  
  15.               
  16.         //系統無法判斷目前Device的方向,有可能是斜置   
  17.         case UIDeviceOrientationUnknown:  
  18.         NSLog(@"未知方向");  
  19.             break;  
  20.               
  21.         case UIDeviceOrientationLandscapeLeft:  
  22.         NSLog(@"螢幕向左橫置");  
  23.             break;  
  24.               
  25.         case UIDeviceOrientationLandscapeRight:  
  26.         NSLog(@"螢幕向右橫置");  
  27.             break;  
  28.               
  29.         case UIDeviceOrientationPortrait:  
  30.         NSLog(@"螢幕直立");  
  31.             break;  
  32.               
  33.         case UIDeviceOrientationPortraitUpsideDown:  
  34.         NSLog(@"螢幕直立,上下顛倒");  
  35.             break;  
  36.               
  37.         default:  
  38.         NSLog(@"無法辨識");  
  39.             break;  
  40.     }  
  41.   
  42.     // Return YES for supported orientations  
  43.     return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); // 只支持向左横向, YES 表示支持所有方向  
  44. }  

0 0
原创粉丝点击