检测景观风景取向变化
我做了一些自定义布局,包括willAnimateRotationToInterfaceOrientation中的动画:持续时间: 我遇到的问题是,如果设备从landscapeLeft更改为landscapeRight,界面应该旋转,但布局代码,尤其是动画应该不会运行。我如何检测到它正在从一个景观变化到另一个景观? self.interfaceOrientation以及[[UIApplication sharedApplication] statusBarOrientation]不返回有效的结果,他们似乎认为设备已经旋转。因此,以下不起作用。检测景观风景取向变化
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation) && UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]) {...}
看来,唯一的解决方案是缓存最后的方向更改。到willAnimateRotationToInterfaceOrientation时:被称为设备和接口方向已被更新。解决方案是在每次更改结束时记录目标方向,以便在方向设置为再次更改时可以查询该值。这并不像我希望的那样优雅(我的视图控制器上还有另一个属性),但据我所知,似乎是唯一的方法。
您可以检查设备的方向,然后设置一个标志,确定您处于左侧还是右侧。然后,当您的设备切换时,您可以抓住它并根据需要进行处理。
要确定方向使用:
if([UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
//set Flag for left
}
else if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
//set Flag for right
}
您还可以赶上的通知时,该设备采用旋转:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
,然后写一个方法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!
}
}
问题是,在调用willAnimateRotationToInterfaceOrientation:时,接口和设备方向已经改变,所以你看不到它来自哪里。 – ima747 2012-07-25 20:07:12
这就是为什么我提到设置一个标志,告诉你你在哪个方向。所以当调用动画时,你可以检查你的标志,并看看你最后的方向。因此,有一个枚举值,名为'previousOrientation',其值选项为'right,left,upsideDown,portrait',并在每次方向更改时更新'previousOrientation'。然后当'willAnimateRotationToInterfaceOrientation:'被调用时,你可以检查'previousRotation'是什么。基本上,我在答案中的“设置标志”的含义就是您在答案中放入的内容,并缓存最后的方向。 – 2012-07-25 20:18:44
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
NSLog(@"Lanscapse");
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
NSLog(@"UIDeviceOrientationPortrait");
}
}
如果不缓存以前的方向值,这似乎不可能。到willAnimateRotationToInterfaceOrientation时:被称为接口和设备方向已经更新,这意味着任何方向已经失去了我对它感兴趣的点。 – ima747 2012-07-25 20:08:40