我想检测设备的当前方向。
所以,我用了一行代码
注意:即使我没有注册它也能给我方向感
-(IBAction) deviceOrientationChanged:(id) sender{
NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}
当我更改方向时,在按钮单击事件中我获得了设备方向。
但是,document说需要先注册,再使用上面的代码。
注意:在下面的代码中,我注册了设备以接收设备方向通知
-(IBAction) deviceOrientationChanged:(id) sender{
if(![[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]){
//This statement is called very time when I click on the button. I expected to execute only once.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}
因此,我无法通过完美的方式获得设备方向。
最佳答案
我认为你的 if 条件是错误的; 你有它作为 如果([[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications]) 应该是
if( ! [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications])
关于ios - 注册 UIDeviceOrientation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13937114/