自定义相机旋转问题
我有一个自定义相机覆盖层,与所有相机视图一样,默认为横向模式。我有一些方向改变逻辑,可以检测方向何时改变,这样我就可以旋转我放置的按钮和视图。另外,我还编辑了 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。我敢打赌这与这里的问题有关。
I have a custom camera overlay that, like all camera views, is default in landscape mode. I have some orientation change logic that detect when the orientation changes so I can rotate the buttons and views that I have placed. Also, I have edited the shouldAutoRotateInterfaceTo method so it does not rotate while the camera is on:
- (BOOL)shouldAutorotateToInterfaceOrientation
(UIInterfaceOrientation)interfaceOrientation {
if (cameraIsOn) {
return NO;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Problem: the camera view still rotates into Portrait mode when I tilt the camera into Landscape. This results in the device being in Landscape position while the view is in Portrait, running off of the screen.
If it helps, here is the method I use to detect orientation changes and transform my buttons:
- (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;
}
Note This seemed to be working ok before I upgraded to iOS 5.
More Info
By looking at my Xib file, I can see that I created the overlay in Portrait mode. When the camera overlay rotates it is moving into Portrait mode while the device is being help in Landscape position.
Even more peculiar, whenever this happens (it does not always happen), I noticed that shouldAutorotateToInderfaceOrientation is not being called in the view controller. I bet that has something to do with the problem here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在我的导航控制器设置上。 UIImagePickerController 将旋转消息发送到根视图控制器的 shouldAutoRotateToInterface: 方法。但是,由于我从旧项目(iOS 3)复制了该类,因此它没有自动复制 shouldAutoRotateToInterface: 方法。现在我在根视图控制器中编写了该方法,旋转问题已解决。
The problem is with my navigation controller setup. UIImagePickerController sends the rotation message to the root view controller's shouldAutoRotateToInterface: method. However, since I copied the class from an older project (iOS 3), it did not have the shouldAutoRotateToInterface: method automatically copied. Now that I wrote the method in my root view controller, to rotation problem is fixed.
当您确实应该使用
UIInterfaceOrientation
时,您似乎正在使用UIDeviceOrientation
。UIDeviceOrientation
的状态比UIInterfaceOrientation
多得多,因此这可能会导致设备状态与您期望的界面状态不同,例如UIDeviceOrientationFaceUp<即使界面方向(对用户而言)看起来应该是
UIInterfaceOrienataionLandscape
,也可以返回 /code>。因此,在您的情况下,您的default
switch case 返回的频率可能比您预期的要高。如果是我,我会将您的
- (void)cameraOrientationChanged
方法更改为- (void)cameraOrientationChangedTo:(UIInterfaceOrientation)orientation
并在shouldAutorotateToInterfaceOrientation< 中调用该方法/code>:
UIDeviceOrientation
与UIInterfaceOrientation
:You seem to be using
UIDeviceOrientation
when you really should be usingUIInterfaceOrientation
.UIDeviceOrientation
has many more states thanUIInterfaceOrientation
, so this could result in a device state being something other than what you were expecting for an interface state, for instanceUIDeviceOrientationFaceUp
can be returned even when the interface orientation (to the user) looks like it should beUIInterfaceOrienataionLandscape
. So in your case, yourdefault
switch case may be returning more often than you would expect for it to.If it were me, I would change your
- (void)cameraOrientationChanged
method to- (void)cameraOrientationChangedTo:(UIInterfaceOrientation)orientation
and call that method inshouldAutorotateToInterfaceOrientation
:UIDeviceOrientation
vsUIInterfaceOrientation
: