这里有两个宏 UIDeviceOrientationIsLandscape
和 UIDeviceOrientationIsPortrait
因此,你可以像这样同时检查它们...
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
或者
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
试一试。
[[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
}
还可以尝试:
UIInterfaceOrientationIsPortrait(orientation)
并且
UIInterfaceOrientationIsLandscape(orientation)
-[UIViewController interfaceOrientation]
这个方法可以获取当前界面的方向。
不过,我们也可以从设备中获取设备方向,而与当前界面的方向无关:
[[UIDevice currentDevice] orientation]
详细信息请参阅文档,但是为了让它起作用,您需要做更多的工作。
- (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);
}
您应该在AppDelegate didFinishLaunchingWithOptions
中放置[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
然后,在您的应用程序的任何位置,您可以使用以下代码获取当前方向:
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
并且可以使用以下代码测试方向:
UIInterfaceOrientationIsPortrait(orientation)
UIInterfaceOrientationIsLandscape(orientation)
UIDeviceOrientationIsLandscape
来测试变量 orientation
,而不是使用 UIInterfaceOrientationIsLandscape
。 - Webdevotion