ios 屏幕旋转大全

来源:互联网 发布:抚仙湖尸库 知乎 编辑:程序博客网 时间:2024/06/08 11:05


如果希望所有界面不可以旋转 则可也在Target —> general 的 device orientation 禁止掉就可以了

或者也可以在AppDelegate中增加,这个方法可以禁止横屏

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  
    {  
         return UIInterfaceOrientationMaskPortrait;  
    }

一般我们都是希望某个页面可以选装例如 图片浏览的页面可以横屏,我这里例子图片浏览界面的图片浏览框架出自(code4app  mj哥的一个demo)

加速计是整个IOS屏幕旋转的基础,依赖加速计,设备才可以判断出当前的设备方向,IOS系统共定义了以下七种设备方向:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {    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};

   以及如下四种界面方向:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft}; 我这里整体是一个tabbarController的程序

1、定义tabbarController的子类

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  BaseTabBarViewController.m  
  3. //  WithBusiness  
  4. //  
  5. //  Created by king on 14-6-18.  
  6. //  Copyright (c) 2014年 sinosoft. All rights reserved.  
  7. //  
  8.   
  9. #import "BaseTabBarViewController.h"  
  10. #import "MJPhotoBrowser.h"  
  11. #import "BaseNavigationViewController.h"  
  12. @interface BaseTabBarViewController ()  
  13.   
  14. @end  
  15.   
  16. @implementation BaseTabBarViewController  
  17.   
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  19. {  
  20.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  21.     if (self) {  
  22.         // Custom initialization  
  23.     }  
  24.     return self;  
  25. }  
  26.   
  27. - (void)viewDidLoad  
  28. {  
  29.     [super viewDidLoad];  
  30.     // Do any additional setup after loading the view.  
  31. }  
  32.   
  33. - (void)didReceiveMemoryWarning  
  34. {  
  35.     [super didReceiveMemoryWarning];  
  36.     // Dispose of any resources that can be recreated.  
  37. }  
  38.   
  39. //6.0之前   
  40. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  41. {  
  42.     if ([[self getTopViewController] isKindOfClass:[MJPhotoBrowser class]]) {  
  43.         return [(MJPhotoBrowser*)[self getTopViewController] shouldAutorotateToInterfaceOrientation:interfaceOrientation];  
  44.     }  
  45.     //禁止旋转  
  46.     return (interfaceOrientation==UIInterfaceOrientationPortrait);  
  47. }  
  48.   
  49. //6.0之后 包括6.0  
  50. - (BOOL)shouldAutorotate  
  51. {  
  52.     if ([[self getTopViewController] isKindOfClass:[MJPhotoBrowser class]]) {  
  53.         return YES;  
  54.           
  55.         //这里我直接return  yes  也可也return 下面这行代码 由topViewController来做控制  
  56. //        return [[self getTopViewController] shouldAutorotate];  
  57.     }  
  58.     return NO;  
  59. }  
  60.   
  61. //  
  62. - (NSUInteger)supportedInterfaceOrientations  
  63. {  
  64.     if ([[self getTopViewController] isKindOfClass:[MJPhotoBrowser class]]) {  
  65.         return [(MJPhotoBrowser*)[self getTopViewController] supportedInterfaceOrientations];  
  66.     }  
  67.     //返回UIDeviceOrientationPortraitUpsideDown, 当在图片浏览界面横屏返回上一页面的时候会自动恢复到竖屏  
  68.     return UIDeviceOrientationPortraitUpsideDown;  
  69. }  
  70.   
  71. -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration  
  72. {  
  73.     return [[self getTopViewController] willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];  
  74. }  
  75.   
  76.   
  77. //获取最顶层的ViewController  
  78. - (id)getTopViewController{  
  79.       
  80.     BaseNavigationViewController *navCtr = (BaseNavigationViewController*)self.selectedViewController;  
  81.       
  82.     BaseUIViewController *viewController = (BaseUIViewController *)navCtr.topViewController;  
  83.     return viewController;  
  84. }  
  85.   
  86.   
  87. @end  

在mjBrowser的类里面

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  2. {  
  3.     if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight  
  4.        ||[[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait  
  5.        ||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft){  
  6.         return YES;  
  7.     }  
  8.     return NO;  
  9. }  
  10.   
  11. - (BOOL)shouldAutorotate  
  12. {  
  13.     if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight  
  14.        ||[[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait  
  15.        ||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft){  
  16.         return YES;  
  17.     }  
  18.     return NO;  
  19. }  
要旋转的时候会调用这个方法

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{  

可以里面做一些横屏操作

判断是否横屏:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeRight  
  2.             ||[[UIDevice currentDevice] orientation]==UIInterfaceOrientationLandscapeLeft) {  
  3.             _screen_change = YES;  
  4.               
  5.         }  
0 0