iOS 屏幕旋转常用方式

来源:互联网 发布:网络许可 英文 编辑:程序博客网 时间:2024/06/01 08:48

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

- (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];
    }
}