UIDevice Orientation

Jor*_*dan 12 iphone cocoa-touch ios-simulator

我在方法中有以下代码.当我在模拟器中运行它时,调试器跳过代码?我错过了什么?

if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) 
{       

} else {

}
Run Code Online (Sandbox Code Playgroud)

srg*_*szy 54

确定界面方向的最佳方法是查看状态栏方向:

 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    if(orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) {

       //Portrait orientation

}

if(orientation == UIInterfaceOrientationLandscapeRight ||
   orientation == UIInterfaceOrientationLandscapeLeft) {

    //Landscape orientation

}
Run Code Online (Sandbox Code Playgroud)

UIDevice class基于加速度计测量方向,如果设备平放,则不会返回正确的方向.


ink*_*ibl 23

需要注意的是有一个宏观UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait,因此而不是分开地比较它LandscapeLeft和LandscapeRight你可能只是不喜欢这样写道:

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
}
Run Code Online (Sandbox Code Playgroud)


Cor*_*oyd 18

更新2

这应该没关系,但尝试打开方向通知:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)

更新

我的坏,我以为它是空的.

尝试删除or语句,然后测试单个方向.看看是否能修复它.也许有一个支架问题或傻事.

我有以下测试在生产代码中工作,所以你的技术应该工作:

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {


}
Run Code Online (Sandbox Code Playgroud)

原始答案

你必须在if块中实际放置语句才能让它进入.

调试器非常智能,可以跳过空块.