自定义相机旋转问题

我有一个自定义相机覆盖,像所有相机视图一样,在横向模式下是默认的。 我有一些方向更改逻辑,可以检测方向何时更改,以便我可以旋转我放置的按钮和视图。 另外,我编辑了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。 我打赌这与这里的问题有关。

问题出在我的导航控制器设置上。 UIImagePickerController将旋转消息发送到根视图控制器的shouldAutoRotateToInterface:方法。 但是,由于我从较旧的项目(iOS 3)复制了该类,因此没有自动复制的shouldAutoRotateToInterface:方法。 现在我在根视图控制器中编写了该方法,以解决旋转问题。

当你真的应该使用UIDeviceOrientation时,你似乎正在使用UIDeviceOrientationUIDeviceOrientation具有比UIInterfaceOrientation更多的状态,因此这可能导致设备状态不是您期望的接口状态,例如,即使接口方向(对用户)看起来应该是UIDeviceOrientationFaceUp也可以返回UIInterfaceOrienataionLandscape 。 因此,在您的情况下,您的default开关案例可能会比您预期的更频繁地返回。

如果是我,我会将你的- (void)cameraOrientationChanged方法更改为- (void)cameraOrientationChangedTo:(UIInterfaceOrientation)orientation并在- (void)cameraOrientationChangedTo:(UIInterfaceOrientation)orientation调用该方法:

 - (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;