2011-11-07 59 views
2

我有一個自定義相機覆蓋圖,與所有相機視圖一樣,在橫向模式下是默認的。我有一些方向更改邏輯,可以檢測方向何時更改,以便我可以旋轉已放置的按鈕和視圖。另外,我已編輯shouldAutoRotateInterfaceTo方法,因此不旋轉,而相機是:自定義相機旋轉問題

- (BOOL)shouldAutorotateToInterfaceOrientation 
       (UIInterfaceOrientation)interfaceOrientation { 
     if (cameraIsOn) { 
      return NO; 
     } 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

問題:攝像機視圖仍然旋轉到肖像模式時,我傾斜攝像機成橫向。這會導致設備處於橫向位置,而視圖處於縱向位置,從屏幕移出。

如果有幫助,這是我用它來檢測方向變化,改造我的按鈕的方法:

- (void)cameraOrientationChanged { 
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
CGAffineTransform transform; 
switch (orientation) { 
    case UIDeviceOrientationPortrait: 
     transform = CGAffineTransformIdentity; 
     self.rotatePrompt.hidden = NO; 
     break; 
    case UIDeviceOrientationPortraitUpsideDown: 
     //transform = CGAffineTransformMakeRotation(180*M_PI/180); 
     transform = CGAffineTransformIdentity; 
     self.rotatePrompt.hidden = NO; 
     break; 
    case UIDeviceOrientationLandscapeLeft: 
     //transform = CGAffineTransformMakeRotation(90*M_PI/180); 
     self.rotatePrompt.hidden = YES; 
     transform = CGAffineTransformIdentity; 
     break; 
    case UIDeviceOrientationLandscapeRight: 
     //transform = CGAffineTransformMakeRotation(-90.0*M_PI/180); 
     transform = CGAffineTransformMakeRotation(180*M_PI/180); 
     self.rotatePrompt.hidden = YES; 
     break; 
    default: 
     transform = CGAffineTransformIdentity; 
     break; 
} 
self.shootButton.transform = transform; 
self.cameraTypeButton.transform = transform; 
self.photoLibraryButton.transform = transform; 
self.changeCameraDirectionButton.transform = transform; 
self.flashButton.transform = transform; 
self.videoTimerLabel.transform = transform; 
self.closeCameraButton.transform = transform; 

}

注意這似乎是工作確定之前,我升級到iOS 5

更多信息 通過查看我的Xib文件,我可以看到我在縱向模式下創建了疊加層。當設備在風景位置幫助時,相機覆蓋層旋轉時將進入縱向模式。

更奇特的是,每當發生這種情況(它並不總是發生),我注意到在視圖控制器中沒有調用shouldAutorotateToInderfaceOrientation。我敢打賭,這跟這個問題有關。

回答

1

問題是與我的導航控制器設置。 UIImagePickerController將旋轉消息發送到根視圖控制器的shouldAutoRotateToInterface:方法。但是,由於我從舊項目(iOS 3)中複製了該類,因此它沒有自動複製shouldAutoRotateToInterface:方法。現在,我在我的根視圖控制器中編寫了該方法,以解決旋轉問題。

0

當你真的應該使用UIInterfaceOrientation時,你似乎正在使用UIDeviceOrientationUIDeviceOrientationUIInterfaceOrientation有更多的狀態,所以這可能會導致設備狀態不是您期望的接口狀態,例如,即使接口方向(對於用戶)看起來應該是接口狀態,也可以返回UIDeviceOrientationFaceUpUIInterfaceOrienataionLandscape。因此,就您的情況而言,您的default開關盒可能會比您預期的更頻繁地返回。

如果是我的話,我想你- (void)cameraOrientationChanged方法更改爲- (void)cameraOrientationChangedTo:(UIInterfaceOrientation)orientation並調用該方法在shouldAutorotateToInterfaceOrientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    [self cameraOrientationChangeTo:interfaceOrientation]; 

    if (cameraIsOn) { 
     return NO; 
    } 

    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

UIDeviceOrientation VS UIInterfaceOrientation

typedef enum { 
    UIDeviceOrientationUnknown, 
    UIDeviceOrientationPortrait, 
    UIDeviceOrientationPortraitUpsideDown, 
    UIDeviceOrientationLandscapeLeft, 
    UIDeviceOrientationLandscapeRight, 
    UIDeviceOrientationFaceUp, 
    UIDeviceOrientationFaceDown 
} UIDeviceOrientation; 

typedef enum { 
    UIInterfaceOrientationPortrait   = UIDeviceOrientationPortrait, 
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, 
    UIInterfaceOrientationLandscapeLeft  = UIDeviceOrientationLandscapeRight, 
    UIInterfaceOrientationLandscapeRight  = UIDeviceOrientationLandscapeLeft 
} UIInterfaceOrientation; 
+0

我的確如你所述。我認爲你的方法更好,因爲我不需要自己創建視圖更改的通知,但是,這並未阻止視圖旋轉。 – nicholjs

+0

你想不要旋轉'self.cameraImagePicker.view'?如果是這樣,刪除'self.cameraImagePicker.view.transform = CGAffineTransformIdentity;'看看是否做了什麼,即使它真的不應該如果視圖控制器告訴視圖不旋轉... – larsacus

+0

也嘗試過。沒有。一些奇怪的東西 - 當我從xcode構建它時,構建在我的設備上正常工作。當我在testflight上發佈應用程序是唯一一次我看到此相機問題。 – nicholjs