Iphone 如何找到模拟器的方向?

Iphone 如何找到模拟器的方向?,iphone,cocoa-touch,Iphone,Cocoa Touch,我正在开发一个应用程序。我想找出iphone的方向。我为这个应用程序编写了代码。有没有办法找到模拟器的方向。这个小片段应该可以在模拟器和物理设备上使用。正如lxt指出的那样,您还需要一些其他的代码来实现它 [UIDevice currentDevice].orientation 例如: if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) NSLog("The device orientatio

我正在开发一个应用程序。我想找出iphone的方向。我为这个应用程序编写了代码。有没有办法找到模拟器的方向。

这个小片段应该可以在模拟器和物理设备上使用。正如lxt指出的那样,您还需要一些其他的代码来实现它

[UIDevice currentDevice].orientation
例如:

if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait)
    NSLog("The device orientation is portrait!");
else
    NSLog("The device orientation is NOT portrait!");

这个小片段应该可以在模拟器和物理设备上使用。正如lxt指出的那样,您还需要一些其他的代码来实现它

[UIDevice currentDevice].orientation
例如:

if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait)
    NSLog("The device orientation is portrait!");
else
    NSLog("The device orientation is NOT portrait!");

Jeremy的回答遗漏了一条关于
[[UIDevice currentDevice]方向]
的极其重要的信息——根据苹果的文档:

除非通过调用BegingeratingDeviceOrientationNotifications启用了方向通知,否则此属性的值始终返回0

在调用
[[UIDevice currentDevice]定向]
之前,必须先调用
[[UIDevice currentDevice]BegingeratingDeviceOrientationNotifications]

完成后,您还应该在某个时候调用
endGeneratingDeviceOrientationNotifications


然而,值得注意的是,模拟器有时无法通过方向更改:我强烈建议在设备上测试方向更改。有一些在模拟器上看不到的边缘情况肯定会发生在实际硬件上。

杰里米的回答缺少一条关于
[[UIDevice currentDevice]orientation]
的极其重要的信息——根据苹果的文档:

除非通过调用BegingeratingDeviceOrientationNotifications启用了方向通知,否则此属性的值始终返回0

在调用
[[UIDevice currentDevice]定向]
之前,必须先调用
[[UIDevice currentDevice]BegingeratingDeviceOrientationNotifications]

完成后,您还应该在某个时候调用
endGeneratingDeviceOrientationNotifications


然而,值得注意的是,模拟器有时无法通过方向更改:我强烈建议在设备上测试方向更改。有一些在模拟器上看不到的边缘情况肯定会发生在实际的硬件上。

同样的情况也适用于模拟器。我根据x、y轴的值找到了方向。下面是我的代码。浮动角度=atan2(yy,xx);基于这个角度,我提到了方向。同样的事情也适用于模拟。我发现方向是基于x,y轴的值。下面是我的代码。浮动角度=atan2(yy,xx);基于这个角度,我提到了方向。见下面我的答案。您需要将此与通知电话相匹配。感谢您指出这一点!我不知道让它工作所需的额外代码片段。请参阅下面我的答案。您需要将此与通知电话相匹配。感谢您指出这一点!我不知道要让它工作需要额外的代码片段。