如何通过UIView引用获取设备朝向?

时间:2023-01-23 14:32:00

I need to get device orientation from a ViewController. I cannot base on:

我需要从视图控制器中获得设备方向。我不能基地:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

because it sometimes returns orientation unknown (for example when the device lays on the table).

因为它有时返回未知的方向(例如当设备放在表上时)。

I just need to know in what orientation is my current UIView displayed (is it landscape left or landscape right). I don't need to have this value updated when orientation changes, I just want to know it, when I ask for it. Just some reference view.orientation ;). Is there something that would help me? I've read UIView documentation, found some reference to UIWindow, but nothing that could help me.

我只需要知道我当前的UIView显示的方向是什么(是左横向还是右横向)。当方向改变时,我不需要更新这个值,我只是想知道,当我要求它的时候。只是一些参考观点。取向;)。有什么能帮助我的吗?我读过UIView文档,找到了一些UIWindow的引用,但是没有任何东西可以帮助我。

4 个解决方案

#1


13  

You can also get the device orientation from UIApplication, have you tried using

你也可以从UIApplication获得设备朝向,你试过了吗

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

#2


4  

UIViewController has a property

ui视图有一个属性

UIInterfaceOrientation interfaceOrientation;

So in your UIViewController, you can access the current orientation of the device at any time via

在你的UIViewController中,你可以随时通过

UIInterfaceOrientation myCurrentOrientation = self.interfaceOrientation;

#3


0  

Swift Version

斯威夫特版本

    let currentOrientation:UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation


    if currentOrientation.isPortrait {

        print("PORTRAIT")

    } else if currentOrientation.isLandscape {

        print("LANDSCAPE")

    }

#4


-1  

Following is the sample code to do the same. There is a variable named deviceOrientation, and it will answer the current orientation of device, whenever being asked.

下面是执行相同操作的示例代码。有一个名为deviceOrientation的变量,无论何时被要求,它都会回答设备当前的方向。

UIDeviceOrientation deviceOrientation;

- (void)viewWillAppear:(BOOL)animated
{
    deviceOrientation = (UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
    [self willAnimateRotationToInterfaceOrientation:deviceOrientation duration:0.5];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    deviceOrientation = (UIDeviceOrientation)interfaceOrientation;
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        NSLog(@"Landscape");
    }
    else
    {
        NSLog(@"Portrait");
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return TRUE;
}

#1


13  

You can also get the device orientation from UIApplication, have you tried using

你也可以从UIApplication获得设备朝向,你试过了吗

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

#2


4  

UIViewController has a property

ui视图有一个属性

UIInterfaceOrientation interfaceOrientation;

So in your UIViewController, you can access the current orientation of the device at any time via

在你的UIViewController中,你可以随时通过

UIInterfaceOrientation myCurrentOrientation = self.interfaceOrientation;

#3


0  

Swift Version

斯威夫特版本

    let currentOrientation:UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation


    if currentOrientation.isPortrait {

        print("PORTRAIT")

    } else if currentOrientation.isLandscape {

        print("LANDSCAPE")

    }

#4


-1  

Following is the sample code to do the same. There is a variable named deviceOrientation, and it will answer the current orientation of device, whenever being asked.

下面是执行相同操作的示例代码。有一个名为deviceOrientation的变量,无论何时被要求,它都会回答设备当前的方向。

UIDeviceOrientation deviceOrientation;

- (void)viewWillAppear:(BOOL)animated
{
    deviceOrientation = (UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
    [self willAnimateRotationToInterfaceOrientation:deviceOrientation duration:0.5];
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    deviceOrientation = (UIDeviceOrientation)interfaceOrientation;
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        NSLog(@"Landscape");
    }
    else
    {
        NSLog(@"Portrait");
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return TRUE;
}