1

我需要知道方向何时从纵向切换到横向,反之亦然。如果我选择听,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;
}

最好的方法是什么?

4

1 回答 1

2

I had similar problems orientating my views when using device orientation. It has up, down, left and right but also unknown and another which I yet to figure it out what it means (int 5 on the enum, 0 being Unknown). Anyways, what I ended up resorting to was to detect the status bar orientation. That remains more consistency for what the user is seeing.

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

Hope this helps.

于 2012-11-01T15:34:05.727 回答