iphone - 查看旋转通知 : why didRotateFromInterfaceOrientation: doesn't get called?

标签 iphone ios uiviewcontroller uiinterfaceorientation

我正在尝试检测任何设备方向变化,以便我可以更新 View 。

无论方向是纵向还是横向,我都想更新 View ,所以我实现了这个方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

我知道如果我想更新 View 以正确显示当前方向,我将需要实现以下一些方法:

– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
– didAnimateFirstHalfOfRotationToInterfaceOrientation:
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

现在,问题是我不明白为什么当我旋转模拟器时这些方法都没有被触发。

我也试过这段代码:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

但还是没有。所以我想知道,模拟器会触发旋转通知吗? 如果是,我做错了什么?

最佳答案

你需要添加类似

的通知观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

并添加方法

- (void) didRotate:(NSNotification *)notification
{   
      UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

       if (orientation == UIDeviceOrientationLandscapeLeft)
      {
        NSLog(@"Landscape Left!");
      }
}

关于iphone - 查看旋转通知 : why didRotateFromInterfaceOrientation: doesn't get called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111013/

相关文章:

java - 我怎样才能使xcode加密字符串与java中的加密字符串相同

iphone - 如何让 youtube iframe 视频在 iOS 上观看时正确调整大小?

ios - 如何减小 UI 选项卡栏项目图像的大小以使其适合选项卡栏?

iOS - Navigationcontroller 与带有导航栏的 Viewcontroller

iphone - 如何让iPhone 4及以下版本的应用兼容iPhone 5?

iphone - Core Data的NSPrivateQueueConcurrencyType以及线程间共享对象

objective-c - iOS - 如何为网络套接字创建读写流?

ios - 为什么 UITableView 在我有 2 个额外的行之前不滚动?

ios - 在倒置肖像模式下关闭 VC 后,主 VC 显示为正常肖像

iphone - 来自 UIView 的 PresentModalViewController