iOS界面横屏竖屏随意切换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/st646889325/article/details/83178429

 APP中所有界面支持竖屏,只有在一个界面,支持横竖屏

横屏竖屏转换的时候,屏幕的大小和控件的尺寸发生了变化,应采用Masonry自动布局的方法

 

手动切换,点击按钮

1. 在AppDelegate中定义一个Rotate,用于记录横竖屏

       @property (nonatomic, assign) NSInteger Rotate;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

               self.Rotate = 0;

}

//当设备横竖屏转换的时候回调用这个方法

- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

{

            //为1的话,说明某个界面需要横竖屏转化

              if (_Rotate == 1) {

                   return UIInterfaceOrientationMaskAll;

              }else {

                 //为0的话,说明只需要竖屏

              return (UIInterfaceOrientationMaskPortrait);

    }

}

// 是否支持设备自动旋转

- (BOOL)shouldAutorotate

{

    if (_Rotate == 1) {

        //为1的话,支持旋转

        return YES;

    }

    return NO;

}

2. 哪个界面要实现此功能,就在哪个界面上

例如自定义一个button,在button的点击事件中做处理

- (void)buttonCilck:(UIButton *)sender
{
    sender.selected =! sender.selected;
    
    if(sender.selected)
    {
        NSlog(@"横屏");
        
        AppDelegate * appdelegate =(AppDelegate *) [UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 1;
        
        //如果强制横屏,打开这段代码,并且将手机设备的竖排方向锁定打开即可
        
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationLandscapeRight];
        }
    }
    else
    {
        NSlog(@"竖屏");
        
        AppDelegate * appdelegate = (AppDelegate *)[UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 0;
        
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationPortrait];
        }
    }
}

- (void)orientationToPortrait:(UIInterfaceOrientation)orientation
{
    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];
    
}

自动切换,push或present界面就是横屏,返回之后是竖屏

1. 方法和上面的方法1一样

2. 在具体的界面实现

- (void)viewWillAppear:(BOOL)animated
{
        [super viewWillAppear:animated];

     AppDelegate * appdelegate =(AppDelegate *) [UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 1;
        
        //如果强制横屏,打开这段代码,,并且将手机设备的竖排方向锁定打开即可 
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationLandscapeRight];
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

       AppDelegate * appdelegate = (AppDelegate *)[UIApplication  sharedApplication].delegate;
        appdelegate.Rotate = 0;
        
        if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
            [self orientationToPortrait:UIInterfaceOrientationPortrait];
        }
}

- (void)orientationToPortrait:(UIInterfaceOrientation)orientation
{
    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];
    
}

猜你喜欢

转载自blog.csdn.net/st646889325/article/details/83178429