Ios 注册UIDeviceOrientation
我想检测设备的当前方向 所以,我使用了一行代码 注意:即使我没有注册,它也会给我方向感Ios 注册UIDeviceOrientation,ios,uidevice,uideviceorientation,Ios,Uidevice,Uideviceorientation,我想检测设备的当前方向 所以,我使用了一行代码 注意:即使我没有注册,它也会给我方向感 -(IBAction) deviceOrientationChanged:(id) sender{ NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]); } 当我更改方向时,在按钮单击事件中,我会获得设备方向 但是,用户说您需要先注册它,然后使用上面的代码 注意:在下面的代码中,我已注册设备以接收设备定向通知
-(IBAction) deviceOrientationChanged:(id) sender{
NSLog(@"Device Orientation= %d",[[UIDevice currentDevice] orientation]);
}
当我更改方向时,在按钮单击事件中,我会获得设备方向
但是,用户说您需要先注册它,然后使用上面的代码
注意:在下面的代码中,我已注册设备以接收设备定向通知
-(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]);
}
因此,我无法找到获取设备方向的完美方法。您无法阻止设备生成
UIViewController
旋转通知。看到问题了吗
您链接到的文档所指的是通知中心的通知。我认为您的if条件是错误的; 你把它当作 如果([[UIDevice currentDevice]正在生成设备定向通知]) 应该是
如果(![[UIDevice currentDevice]正在生成设备定向通知])注册通知与生成通知不同。通常,如果您计划在应用程序代理中或在适当的位置使用设备定向通知,您将在UIDevice上发出呼叫BegingeratingDeviceOrientationNotifications,并在您不再需要这些通知时在另一个适当的位置发出endGeneratingDeviceOrientationNotifications。这只是DeviceOrientIon通知的初始设置 要获得这些通知,您必须向通知中心发送控制器或对象。U注册通知UIDeviceOrientationIDChangeNotification。 [[NSNotificationCenter defaultCenter]添加观察者:myObject选择器:@selector(deviceRotated:)名称:UIDeviceOrientationIDChangeNotification对象:nil]
并在不需要时将其从通知中心或对象的dealloc中删除。它表示“除非通过调用BegingeratingDeviceOrientationNotifications启用了方向通知,否则此属性的值始终返回0。”其中属性指向UIDeviceHNKs的“方向”属性,我已经更正了代码,但现在它没有进入注册设备的if条件。我不知道为什么它会变成真的。我已经签入了我的全部代码,没有其他地方可以从设备定向进行注册。注册通知与生成通知不同。通常,如果您计划在应用程序代理中或在适当的位置使用设备定向通知,您将在UIDevice上发出呼叫BegingeratingDeviceOrientationNotifications,并在您不再需要这些通知时在另一个适当的位置发出endGeneratingDeviceOrientationNotifications。这只是DeviceOrientIon通知的初始设置。