我的目标是使用按钮更改锁定/解锁方向 . 在锁定模式下,可以通过按钮切换方向,而在解锁模式下,方向由传感器决定 .
我尝试使用 [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:orientation] forKey:@"orientation"]
实现此目的,但此代码存在问题 .
假设我的应用程序目前是'Lock mode, portrait UI, portrait device' . 然后我将设备旋转到横向左侧,并解锁方向 . 我预计 [[UIDevice currentDevice] orientation]
应该是 UIDeviceOrientationLandscapeLeft
. 但 Value 是 UIDeviceOrientationPortrait
,虽然真正的设备是景观!
我还尝试了通知中心,代码如下 .
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDeviceOrientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
但是这段代码不能正常工作 . 如果设备处于横向,并且 [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationPortrait]
将UI方向设置为纵向,则在将设备旋转为纵向时不会调用 onDeviceOrientationChanged
. 我认为设备的方向值已经设置为纵向(通过我的代码,而不是传感器) .
PS . 当然,我检查了 Required full screen 选项 .
EDIT :知道如何获得不受 [[UIDevice currentDevice] setValue]
影响的设备真实方向也是一个很好的答案 .
1 回答
我用
MotionManager
解决了这个问题 . 当我锁定(或改变)方向时,[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:orientation] forKey:@"orientation"]
像往常一样 .但是,当我解锁方向时,我使用下面的代码使用
MotionManager
刷新了方向 .当然,您应该知道
supportedInterfaceOrientations
和shouldAutorotate
方法 .