自定义相机旋转问题

3

我有一个自定义相机叠加层,像所有相机视图一样,默认为横向模式。我有一些方向变化逻辑,可以检测方向变化,以便旋转我放置的按钮和视图。此外,我已编辑了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个回答

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

0

你似乎在使用 UIDeviceOrientation,但你真正应该使用的是 UIInterfaceOrientationUIDeviceOrientationUIInterfaceOrientation 有更多的状态,因此这可能导致设备状态与你期望的界面状态不同,例如,即使用户界面方向看起来应该是 UIInterfaceOrienataionLandscape,也可能返回 UIDeviceOrientationFaceUp。所以在你的情况下,你的 default switch case 可能会比你预期的更频繁地返回。

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {

    [self cameraOrientationChangeTo:interfaceOrientation];

    if (cameraIsOn) {
        return NO;
    }

    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

UIDeviceOrientationUIInterfaceOrientation的区别:

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

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

我按照你所描述的做了。我认为你的方法更好,因为我不需要自己为视图更改创建通知,但是这并没有阻止视图旋转。 - nicholjs
你是想不旋转 self.cameraImagePicker.view 吗?如果是的话,可以删除 self.cameraImagePicker.view.transform = CGAffineTransformIdentity; 然后看看是否有任何变化,尽管如果视图控制器告诉视图不要旋转,这实际上不应该发生任何变化... - larsacus
我也尝试了这个方法,但没用。有点奇怪的是,在我使用Xcode进行构建时,该构建在我的设备上运行良好。只有当我在TestFlight上发布应用程序时,才会出现这个相机问题。 - nicholjs
尝试在调试模式下的编译设置中打开优化。我听说过在LLVM和iOS5上使用优化会出现一些问题。 - larsacus
我也尝试过了。我的调试版本仍然可以正常工作。这是一个非常令人沮丧的问题 :( - nicholjs
我添加了一些关于我遇到的问题的更多信息。任何想法都会有所帮助。 - nicholjs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接