我需要知道何时从肖像切换到景观,反之亦然。如果我选择收听UIDeviceOrientationDidChangeNotification
,我会面朝上,面朝下等,但是我只需要在接口旋转时运行代码。
我可以做http://the.ichibod.com/kiji/how-to-handle-device-rotation-for-uiviews-in-ios/
- (void)deviceOrientationDidChange:(NSNotification *)notification {
//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Ignoring specific orientations
if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown || currentOrientation == orientation) {
return;
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(relayoutLayers) object:nil];
//Responding only to changes in landscape or portrait
currentOrientation = orientation;
}
最好的方法是什么?
我在使用设备方向时遇到了类似的问题。它具有向上,向下,左和右,但也未知,我还没有弄清楚它的含义(枚举5,0是未知)。无论如何,我最终诉诸于检测状态栏的方向。对于用户所看到的东西,这仍然更加一致。
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
希望这会有所帮助。