iOS帶有Camera 的View 手動(dòng)及自動(dòng)設(shè)置屏幕方向匯總(iOS orietation video rotate)

帶有Camera 的View 手動(dòng)及自動(dòng)設(shè)置屏幕方向匯總

需求:

1. 在一個(gè)始終打開Camera的View上,默認(rèn)以橫屏進(jìn)入,橫屏狀態(tài)下可上下旋轉(zhuǎn)自動(dòng)變換方向,手動(dòng)點(diǎn)擊按鈕切換豎屏,豎屏默認(rèn)只有Home鍵向下一種方向。

2. 部分控件橫豎屏下的位置差別較大(使用AutoLayout實(shí)現(xiàn)) 不在本文介紹,如需幫助請(qǐng)點(diǎn)擊AutoLayout 實(shí)現(xiàn)橫豎屏位置差別較大的布局

注意:帶有Camera的View在旋轉(zhuǎn)時(shí)需要考慮先旋轉(zhuǎn)屏幕方向再旋轉(zhuǎn)Camera的Video方向,兩者并不直接綁定,因此需要我們分開做旋轉(zhuǎn)操作,否則會(huì)出現(xiàn)屏幕方向與相機(jī)的Video位置不能完全重合。


GitHub地址(附代碼) : 帶有Camera 的View 手動(dòng)及自動(dòng)設(shè)置屏幕方向匯總

簡(jiǎn)書地址 : 帶有Camera 的View 手動(dòng)及自動(dòng)設(shè)置屏幕方向匯總

博客地址 : 帶有Camera 的View 手動(dòng)及自動(dòng)設(shè)置屏幕方向匯總

掘金地址 : 帶有Camera 的View 手動(dòng)及自動(dòng)設(shè)置屏幕方向匯總


總體流程:

  • 主控制器實(shí)現(xiàn)- (UIInterfaceOrientationMask)supportedInterfaceOrientations設(shè)置支持旋轉(zhuǎn)的方向
  • xib中設(shè)置每個(gè)控件橫豎屏下不同的約束
  • 通過按鈕手動(dòng)設(shè)置屏幕將切換到的方向,并設(shè)置相機(jī)Video畫面的方向
  • 設(shè)置屏幕不同方向的需要改變的控件的尺寸狀態(tài)等相關(guān)邏輯
1. 在主控制器中首先加載支持屏幕方向的方法
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    UIInterfaceOrientationMask screenMode;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isPortraitMode"]) {
        g_isPortraitMode = true;
        screenMode = UIInterfaceOrientationMaskPortrait;
    }else {
        g_isPortraitMode = false;
        screenMode = UIInterfaceOrientationMaskLandscape;
    }
    
    return screenMode;
}

當(dāng)程序啟動(dòng)后,系統(tǒng)會(huì)先調(diào)用此方法,在- (UIInterfaceOrientationMask)supportedInterfaceOrientations中,通過全局變量flag標(biāo)識(shí)設(shè)備支持旋轉(zhuǎn)的方向,這里我們?cè)O(shè)置如果是豎屏則只支持Home鍵向下的情況,如果是橫屏則支持上下旋轉(zhuǎn)的兩種橫屏方向,該方法會(huì)在屏幕每次旋轉(zhuǎn)時(shí)調(diào)用。

2. 使用Autolayout 設(shè)置xib中控件的布局

如果是要實(shí)現(xiàn)橫豎屏上的部分控件位置差別比較大可參考以下文章,
如需幫助請(qǐng)點(diǎn)擊SizeClass

3. 手動(dòng)設(shè)置屏幕方向
- (void)setScreenOrientation:(UIInterfaceOrientation)orientation {
     // m_rotatestate = orientation;   // 本行為我們自己項(xiàng)目中標(biāo)致旋轉(zhuǎn)方向的全局變量
    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 = orientation;//這里可以改變旋轉(zhuǎn)的方向
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

當(dāng)點(diǎn)擊轉(zhuǎn)換按鈕的時(shí)候可通過上面的代碼將屏幕手動(dòng)旋轉(zhuǎn)為豎屏/橫屏方向。

4.設(shè)置控件的狀態(tài)

注意:要確保先設(shè)置了屏幕方向后再更新部分控件的位置或是狀態(tài),不然可能獲取部分控件時(shí)調(diào)用控件的尺寸為上一種屏幕方向時(shí)控件的尺寸導(dǎo)致出現(xiàn)異常情況。

注意:

  • 因?yàn)楫?dāng)APP退出時(shí)下次進(jìn)入會(huì)使用上一次保存的橫屏或豎屏狀態(tài),所以將橫豎屏的值保存在數(shù)據(jù)庫
  • 當(dāng)切換橫豎屏?xí)r,首先設(shè)置屏幕方向,請(qǐng)注意屏幕方向和相機(jī)方向并不直接關(guān)聯(lián),所以每次旋轉(zhuǎn)需要分別旋轉(zhuǎn),然后設(shè)置相機(jī)Video的方向,即如下代碼,相機(jī)的方向并不會(huì)隨著屏幕方向變換而自動(dòng)切換,所以需要同步進(jìn)行設(shè)置。
-(void)adjustAVOutputDataOrientation:(AVCaptureVideoOrientation)aOrientation
{
    for(AVCaptureConnection *connection in video_output.connections)
    {
        for(AVCaptureInputPort *port in [connection inputPorts])
        {
            if([[port mediaType] isEqual:AVMediaTypeVideo])
            {
                if([connection isVideoOrientationSupported])
                {
                    [connection setVideoOrientation:aOrientation];
                }
            }
        }
    }
}
  • 注冊(cè)[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil]; 當(dāng)方向旋轉(zhuǎn)過以后會(huì)調(diào)用一下方法,可進(jìn)行部分邏輯的實(shí)現(xiàn)
- (void)deviceOrientationDidChange:(NSNotification *)notification {
    //Obtaining the current device orientation
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    
    NSLog(@"Curent UIInterfaceOrientation is %ld",(long)orientation);
    
    //Ignoring specific orientations
    if (g_isPortraitMode || orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown  || orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown){
        return;
    }
    if(orientation == UIDeviceOrientationLandscapeLeft)
        NSLog(@"Device Left");
    if(orientation == UIDeviceOrientationLandscapeRight)
        NSLog(@"Device Right");
    [self rotateToInterfaceOrientation:(UIInterfaceOrientation)orientation];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容