iOS 全局豎屏 單個viewcontroller點擊按鈕支持橫屏


注:由於當時的需求需要本文描述的方法只是視圖旋轉並不是真正的自動旋屏(不建議采用),末尾的第二個demo才是真正的自動旋屏且已正常上線APP,親測有效。

問題描述:項目工程只支持豎屏,在播放器頁面需要點擊按鈕進行橫豎屏切換,並能根據手機的方向進行自動旋轉

如圖:只勾選了豎屏

解決方法:(主要是采用視圖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];

    }
    

}

4.程序運行效果

豎屏效果:

 

橫屏效果:

簡單的demo下載地址(未作ui):http://download.csdn.net/user/wusangtongxue

(更新)第二次修改后的真正橫屏demo地址:http://download.csdn.net/download/wusangtongxue/10049935


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM