iOS 全局竖屏 单个viewcontroller点击按钮支持横屏

问题描述:项目工程只支持竖屏,在播放器页面需要点击按钮进行横竖屏切换,并能根据手机的方向进行自动旋转

如图:只勾选了竖屏

解决方法:(主要是采用视图transform的原理 横屏时调整视频视图上的每个控件坐标位置 竖屏在调整回去)

1.电池状态栏的显示与隐藏

首先在plist文件中添加View controller-based status bar appearance 设置为no

然后在控制器中通过[[UIApplication sharedApplication] setStatusBarHidden: withAnimation:NO]方法

[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];

来显示或者隐藏状态栏,一般情况下竖屏时状态栏会显示 横屏则隐藏

导航栏也进行隐藏换成自己的头部视图条

self.navigationController.navigationBarHidden = YES;

2.通知监测状态栏方向(自动旋转屏幕)

设置全局的orientation:

orientation = [UIDevice currentDevice].orientation;

通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

方法:

复制代码
- (void)orientChange:(NSNotification *)noti {
//    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;

    orientation = [UIDevice currentDevice].orientation;

    NSLog(@"打印现在的设备方向:%ld",(long)orientation);
    switch (orientation)
    {
        case UIDeviceOrientationPortrait: {
            NSLog(@"屏幕竖直");
            isFullScreen = NO;
            [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
//            [UIView animateWithDuration:0.25 animations:^{
//                playView.transform = CGAffineTransformMakeRotation(0);
//                playView.frame = CGRectMake(0, 0, widthAll, 300);
//                topView.frame = CGRectMake(0, 0, widthAll, 50);
//                footView.frame = CGRectMake(0, 250, widthAll, 50);
//                menuBtn.frame = CGRectMake(widthAll-100, 10, 100, 30);
//            }];
            [self shupinAction];
        }
             break;

        case UIDeviceOrientationLandscapeLeft: {
            
            isFullScreen = YES;
            NSLog(@"屏幕向左转");
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
            [self rightHengpinAction];
//            [UIView animateWithDuration:0.25 animations:^{
//                playView.transform = CGAffineTransformMakeRotation(M_PI*0.5);
//                playView.frame = CGRectMake(0, 0, widthAll, heightAll);
//                topView.frame = CGRectMake(0, 0, heightAll, 50);
//                footView.frame = CGRectMake(0, widthAll-50, heightAll, 50);
//                menuBtn.frame = CGRectMake(heightAll-100, 10, 100, 30);
//            }];
        }
            break;
        case UIDeviceOrientationLandscapeRight: {
            isFullScreen = YES;
            NSLog(@"屏幕向右转");
            [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
            [self leftHengpinAction];
//            [UIView animateWithDuration:0.25 animations:^{
//                playView.transform = CGAffineTransformMakeRotation(-M_PI*0.5);
//                playView.frame = CGRectMake(0, 0, widthAll, heightAll);
//                topView.frame = CGRectMake(0, 0, heightAll, 50);
//                footView.frame = CGRectMake(0, widthAll-50, heightAll, 50);
//                menuBtn.frame = CGRectMake(heightAll-100, 10, 100, 30);
//            }];
        }
            break;
        default:
            break;
    }
}
复制代码

3.点击按钮进行横竖屏切换

添加按钮,设置点击方法

复制代码
-(void)rotateScreeen:(UIButton *)btn
{
    NSLog(@"旋转屏幕");
    btn.selected = !btn.selected;
    if (btn.selected) {
        isFullScreen = YES;
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
        
        if (orientation == UIDeviceOrientationPortrait) {
            NSLog(@"当前竖屏模式");
            [self rightHengpinAction];
        }else if(orientation == UIDeviceOrientationLandscapeLeft)
        {
            NSLog(@"当前左横屏模式");
            [self shupinAction];
        }else if(orientation == UIDeviceOrientationLandscapeRight)
        {
            NSLog(@"当前右横屏模式");
            [self shupinAction];
        }
        
        
    }else{
        isFullScreen = NO;
       [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
        [self shupinAction];

    }
    

}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值