2011-11-07 57 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