iOS屏幕旋转

方向分两种,一种是设备的方向,一种是视图方向。设备方向有两种方式可以改变,一个是通过重力加速计,即旋转屏幕的方式去改变,一个是通过代码
自然屏幕旋转就会有多种方式,比如:通过旋转window实现,通过旋转view实现,通过旋转layer实现,通过UIInterfaceOrientationMask实现。

一、直接设置 UIDevice 的 orientation(不推荐,可能被和谐)
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {  
    [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];  
}
二、所有页面横屏(可使用 Interface Builder工具设计界面)
// 配置plist
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
三、调整window的方向,这样就不用每个view都改变了
// 重写方法四中方法为不允许自动
UIApplication *application=[UIApplication sharedApplication];
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);
四、重写方法(支持重力感应)

TabbarController中

#pragma mark - 控制旋转屏幕
#pragma mark 支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}
#pragma mark 是否支持自动旋转
- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

NavigationController中

#pragma mark - 控制旋转屏幕
#pragma mark 支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}
#pragma mark 是否支持自动旋转
- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

某个控制器支持旋转(不支持则改变返回值)

#pragma mark - 控制旋转屏幕
#pragma mark 支持旋转的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
#pragma mark 是否支持自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
五、设备旋转+视图旋转(最终我项目采用的方案)
#pragma mark - 控制旋转屏幕
#pragma mark 1.旋转设备
- (void)interfaceOrientation:(BOOL)rotate
{
    UIInterfaceOrientation orientation = rotate ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait;
    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];
    }
    [[UIApplication sharedApplication] setStatusBarOrientation:orientation];
    [[UIApplication sharedApplication] setStatusBarHidden:rotate];
}

#pragma mark 2.旋转视图
- (void)rotateView:(BOOL)rotate
{
    self.view.transform = CGAffineTransformMakeRotation(rotate ? M_PI_2 : 0);
    self.view.bounds = [UIScreen mainScreen].bounds;
}

#pragma mark 3.重新布局
- (void)reLayoutContentViews
{
    
}
是否允许旋转

Appdelagate.h中

@property (nonatomic,assign)NSInteger allowRotate;

Appdelegate.m中

#pragma mark - 控制旋转屏幕
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (_allowRotate == 1)
    {
        return UIInterfaceOrientationMaskAll;
    }
    else
    {
        return (UIInterfaceOrientationMaskPortrait);
    }
}

需要旋转功能的控制器中

- (void)viewWillAppear:(BOOL)animated
{
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 1;
}

- (void)viewWillDisappear:(BOOL)animated
{
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 0;
// 防止pop到上一级界面仍然横屏
    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 = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
} 


iOS屏幕自动旋转问题 以及横屏模式打开APP出现的问题
iOS监听横竖屛通知

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容