我有一个 OpenGL 游戏需要了解设备的方向(即纵向与横向)。
API 看起来很简单......我的 ViewController 的相关部分是:
@implementation MyViewController
- (id)init
{
...
// Get orientation events
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
...
}
-(void)didRotate:(NSNotification*)notification
{
NSLog(@"didRotate %d", orientation);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
NSLog(@"shouldAutorotateToInterfaceOrientation %d", orientation);
return YES;
}
@end
发生的事情是我在启动期间接到了几个电话:
shouldAutorotateToInterfaceOrientation 3
shouldAutorotateToInterfaceOrientation 3
shouldAutorotateToInterfaceOrientation 3
shouldAutorotateToInterfaceOrientation 3
didRotate 4
shouldAutorotateToInterfaceOrientation 4
shouldAutorotateToInterfaceOrientation 4
didRotate 1
shouldAutorotateToInterfaceOrientation 1
设置好 UIView 和 EAGLContext 之后,它开始渲染,一切看起来都很好,但是 shouldAutorotateToInterfaceOrientation 和 didRotate 再也不会被调用,无论我在玩游戏时旋转了多少手机。
发生这种情况我做错了什么?