iOS横竖屏

如今开发的APP大部分界面是竖屏的,只有视频播放的界面和webview阅读文字的界面是能够横屏操做的。web

那么就进行以下处理:ide

  一、首先确保APP支持横屏旋转测试

  二、个人App里面都是走UINavigationController进行界面push切换的,因此首先建立一个UINavigationController的子类,并设定容许转屏:  动画

  

#pragma mark 转屏方法重写
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
-(BOOL)shouldAutorotate{
    return self.visibleViewController.shouldAutorotate;
}

在不想转屏切换的ViewController上重写如下方法:spa

  

#pragma mark 转屏方法 不容许转屏
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate
{
    return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return NO;
}

在想转屏切换的ViewController上能够照这样重写(容许左右横屏以及竖屏):code

  

- (BOOL)shouldAutorotate {
    return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

另外,在ViewController中对于转屏事件能够参见下面的方法进行捕获:视频

  

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        //计算旋转以后的宽度并赋值
        CGSize screen = [UIScreen mainScreen].bounds.size;
        //界面处理逻辑
        self.lineChartView.frame = CGRectMake(0, 30, screen.width, 200.0);
        //动画播放完成以后
        if(screen.width > screen.height){
            NSLog(@"横屏");
        }else{
            NSLog(@"竖屏");
        }
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        NSLog(@"动画播放完以后处理");
    }];
}

区分当前屏幕是否为横竖屏的状态,其实经过判断当前屏幕的宽高来决定是否是横屏或者竖屏:blog

竖屏时:宽<高事件

横屏时:宽>高webview

以上在IOS八、9中测试经过

相关文章
相关标签/搜索