自定义相机旋转问题

时间:2011-11-07 16:24:36

标签: iphone ios camera rotation

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

当您真正使用UIDeviceOrientation时,您似乎正在使用UIInterfaceOrientationUIDeviceOrientation的状态多于UIInterfaceOrientation,因此这可能导致设备状态不是您期望的接口状态,例如UIDeviceOrientationFaceUp即使在界面方向(对用户)看起来应该是UIInterfaceOrienataionLandscape。因此,在您的情况下,您的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;