检测景观到横向方向的变化

ima*_*747 0 orientation uiviewcontroller ios

我做了一些自定义布局,包括willAnimateRotationToInterfaceOrientation中的动画:持续时间:我遇到的问题是,如果设备从landscapeLeft更改为landscapeRight,界面应该旋转但布局代码,尤其是动画不应该运行.如何检测到它从一个景观变为另一个景观?self.interfaceOrientation以及[[UIApplication sharedApplication] statusBarOrientation]不返回有效结果,他们似乎认为设备已经旋转.结果以下不起作用.

if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) {...}
Run Code Online (Sandbox Code Playgroud)

Jus*_*son 5

您可以检查设备方向,然后设置一个标记,指示您是左向还是右向.然后,当您的设备切换时,您可以抓住它并随意处理它.

确定方向使用:

if([UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
    //set Flag for left
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
    //set Flag for right
}
Run Code Online (Sandbox Code Playgroud)

您还可以在设备旋转时捕获通知:

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

然后编写一个detectOrientation类似的方法:

-(void) detectOrientation 
{
    if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
    {
        //Set up left
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
    {
        //Set up Right
    } else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    {
        //It's portrait time!
    }   
}
Run Code Online (Sandbox Code Playgroud)