ios 如何在 iPhone 中以编程方式检查设备的方向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3600097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:37:59  来源:igfitidea点击:

How to check the orientation of device programmatically in iPhone?

iphoneobjective-ciosipad

提问by suse

Possible Duplicate:
iPhone orientation

可能重复:
iPhone 方向

How to check the orientation of device at any point off time programmatically in iPhone ?

如何在 iPhone 中以编程方式在任何时间点检查设备的方向?

Thank You.

谢谢你。

回答by Azhar

Here are macros UIDeviceOrientationIsLandscapeand UIDeviceOrientationIsPortrait

这里是宏UIDeviceOrientationIsLandscapeUIDeviceOrientationIsPortrait

so rather checking separately you can do it like this ...

所以宁愿单独检查,你可以这样做......

   if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
         // code for landscape orientation      
    }

OR

或者

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
         // code for Portrait orientation       
    }

回答by Sanket Pandya

Try it.

尝试一下。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

if ( ([[UIDevice currentDevice] orientation] ==  UIDeviceOrientationPortrait)  )
{
   // do something for Portrait mode
}
else if(([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) || ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationLandscapeLeft))
{
   // do something for Landscape mode
}

回答by AlexVogel

Try also:

也试试:

UIInterfaceOrientationIsPortrait(orientation)

and

UIInterfaceOrientationIsLandscape(orientation)

回答by Joost

-[UIViewController interfaceOrientation]

-[UIViewController interfaceOrientation]

However it is possible to retrieve the deviceOrientation, independent from the current interface orientation:

但是,可以独立于当前界面方向来检索 deviceOrientation:

[[UIDevice currentDevice] orientation]

[[UIDevice currentDevice] orientation]

Read the documentation for more information though, you need to do some more things to make that work.

阅读文档以获取更多信息,但您需要做更多的事情才能使其工作。

回答by kumar123

You can also check within the function

您也可以在函数内检查

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations

    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       //do for portrait
    }       
    else 
    { 
       //do for Landscape 
    }
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

回答by Kyle

You should place [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];in your AppDelegate didFinishLaunchingWithOptions

你应该把[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];你的AppDelegate didFinishLaunchingWithOptions

Then, anywhere in your application you can get the current orientation with:

然后,您可以在应用程序的任何位置获取当前方向:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

And test orientation with:

并测试方向:

UIInterfaceOrientationIsPortrait(orientation)UIInterfaceOrientationIsLandscape(orientation)

UIInterfaceOrientationIsPortrait(orientation)UIInterfaceOrientationIsLandscape(orientation)