ios - 注册 UIDeviceOrientation

标签 ios uidevice uideviceorientation

我想检测设备的当前方向。

所以,我用了一行代码

注意:即使我没有注册它也能给我方向感

-(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/

相关文章:

iphone - 获取设置 View 的当前方向的最佳方法是什么

iphone - UIDeviceOrientation 在 iphone 中面朝下

ios - 防止 UIViewController 在模态旋转时旋转

ios - 为什么 UIView(在 iPad 上)默认为纵向,即使在 Storyboard 中设置为横向

objective-c - iOS 上的 Cocos2d : 2d billiard zooming

iOS Firebase 推送通知 : How To Give Firebase User's Device Token And Send Notification

objective-c - 从所有者拥有的对象调用方法

ios - 删除时多个原型(prototype)单元格标题会移动

c# - 调用 MonoTouch.UIKit.UIDevice.CurrentDevice.SystemVersion 时发生崩溃

iPhone 4 屏幕尺寸不正确?