分享

iOS开发 Orientation 屏幕旋转

 玄冰优 2015-12-08

现在随着手机屏幕的增大,很多项目都是竖屏控制的,但是,也有需要横屏的时候,现在将方法整理一下。

1 项目中General 设置,图1是iOS7之前的版本,图2是iOS7之后的版本。
 
 



2 代码枚举值
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the topUIDeviceOrientationLandscapeLeft,    // 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   */
 
3 控制旋转的方法
-(BOOL)shouldAutorotate
{
//   return  NO;//不允许随着设备旋转
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations{  
    return UIInterfaceOrientationMaskAllButUpsideDown; 
//
直接返回支持的旋转方向,该方法在iPad上的默认返回值是UIInterfaceOrientationMaskAll,
//iPhone上的默认返回值是UIInterfaceOrientationMaskAllButUpsideDown

 }

4 项目实现方式
A. 强制横屏 选择了旋转屏幕View
-(void)buildStageRotate
{
    self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
}
B.监测设备的变化
在- (void)viewDidLoad {}里面添加观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
实现效果
-(void)orientChange:(NSNotification *)notify
{
    UIDeviceOrientation oriente = [UIDevice currentDevice].orientation;
    switch (oriente) {
        case UIDeviceOrientationPortrait:
        {
            NSLog(@"正常竖屏");
        }
            break;
        case UIDeviceOrientationLandscapeLeft:
        {
             NSLog(@"home在右");
            CGRect frame = _webShow.frame;
            frame.size.height = kUIHeight;
            frame.size.width = kUIWidth;
            _webShow.frame = frame;
        }
            break;
        case UIDeviceOrientationLandscapeRight:
        {
            NSLog(@"home在左");
        }
            break;
           
        default:
            break;
    }
}

#define kUIHeight  [[UIScreen mainScreen]bounds].size.height
#define kUIWidth   [[UIScreen mainScreen]bounds].size.width
旋转时候的大小设定不能使用 frame  要使用 bounds
其中subviews 一定要autoLayout,否则只是单纯的转屏,并没有实现横屏展示的效果
PS参考: 
http://www./industry/20140813/9373.html
http://blog.csdn.net/zzfsuiye/article/details/8251060
http://www.cnblogs.com/jhzhu/p/3480885.html
后续看到非常详细的介绍
http://www.jb51.net/article/72826.htm

----------------------------------
--------------------------------
2017-03-01 修改完善 UIInterfaceOrientation
旋转屏幕,利用状态条位置,进行页面的旋转

---
首先设置,屏幕锁定
-(BOOL)shouldAutorotate{
    return NO;
}
其次,设置为点击按钮,屏幕竖屏和横屏进行切换
{
 NSLog(@"正常竖屏");
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
            CGRect frame = self.showWeb.frame;
            frame.size.height = [[UIScreen mainScreen]bounds].size.height;
            frame.size.width = [[UIScreen mainScreen]bounds].size.width;
           
            self.showWeb.transform = CGAffineTransformIdentity;
            self.showWeb.frame = frame;
             NSLog(@"nomal frame is %@",NSStringFromCGRect(self.showWeb.frame));
}
~~~~~~~~~~~
{
            NSLog(@"home在右,页面旋转");
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
            CGRect frame = self.showWeb.frame;
            frame.size.height = [[UIScreen mainScreen]bounds].size.width;
            frame.size.width = [[UIScreen mainScreen]bounds].size.height;
            self.showWeb.transform = CGAffineTransformMakeRotation(M_PI/2);//旋转
            self.showWeb.frame = frame;
            NSLog(@"right frame is %@",NSStringFromCGRect(self.showWeb.frame));
        }

----------------------------
旋转之后,状态栏是隐藏状态的,修改方式

1.在info.plist文件中,添加View controller-based status bar appearance,属性为bool,设为NO;
2 在
application:didFinishLaunchingWithOptions:中添加以下下面代码:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

----------------------------



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多