开发者

How to check the orientation of device programmatically in iPhone? [duplicate]

This question already has answers here: Clos开发者_运维百科ed 10 years ago.

Possible Duplicate:

iPhone orientation

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

Thank You.


Here are macros UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait

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       
    }


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
}


Try also:

UIInterfaceOrientationIsPortrait(orientation)

and

UIInterfaceOrientationIsLandscape(orientation)


-[UIViewController interfaceOrientation]

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

[[UIDevice currentDevice] orientation]

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


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);
}


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

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

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

And test orientation with:

UIInterfaceOrientationIsPortrait(orientation) UIInterfaceOrientationIsLandscape(orientation)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜