iOS 屏幕旋转常用方式

//此方法在进入应用和当屏幕旋转的额时候,会调用

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if (self.direction == SuportDirectionRight) {
        return UIInterfaceOrientationMaskLandscapeRight;
    } else if (self.direction == SuportDirectionPortrait){
        return UIInterfaceOrientationMaskPortrait;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}
    //这一步,是必须的,因为在进入目标的VC之后,需要回调上述的APPdelegate方法
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.direction = SuportDirectionRight;

     UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewController *VC = [board instantiateViewControllerWithIdentifier:@"ViewController"];
//必须将目标的VC设置为navigationVC,如果前一个VC为navigationVC就不用这一步
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:VC];
    nav.navigationBarHidden = YES;
    [self presentViewController:nav animated:NO completion:^{
        noCurrentVC = NO;
    }];


- (BOOL)prefersStatusBarHidden {
    return YES;
}

-(BOOL)shouldAutorotate {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}

强制屏幕旋转

#pragma - mark  进入全屏

-(void)begainFullScreen {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.shouldChangeOrientation = YES;
    
    [[UIDevice currentDevice] setValue:@"UIInterfaceOrientationLandscapeLeft" forKey:@"orientation"];
    
    int count = [UIApplication sharedApplication].windows.count;
    NSLog(@"%@", [UIApplication sharedApplication].windows.lastObject.subviews.firstObject);
    NSLog(@"key=%@",[UIApplication sharedApplication].windows);
    //强制zhuan'p:
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)] && count==4) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationLandscapeLeft;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}


#pragma - mark 退出全屏
-(void)endFullScreen {
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.shouldChangeOrientation = NO;
    
    //强制归正:
    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];
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值