我有一个带标签栏的应用程序,每个标签中都有导航控制器.当用户摇动设备时,UIImageView在导航控制器中显示为子视图.但UIImageView必须包含特殊图像,具体取决于设备的当前方向.
如果我写的只是
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
//Code
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight||interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//Code
}
}
Run Code Online (Sandbox Code Playgroud)
如果用户在摇动前旋转设备,视图就会变得疯狂.
有没有办法让iPhone出现当前定位?
Azh*_*har 49
这是宏UIDeviceOrientationIsLandscape和UIDeviceOrientationIsPortrait
所以相反单独检查你可以这样做......
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code for landscape orientation
}
Run Code Online (Sandbox Code Playgroud)
要么
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code for Portrait orientation
}
Run Code Online (Sandbox Code Playgroud)
Jim*_*rue 17
正如beno所说,如果您在视图中尽早检测到方向,这似乎是一个更好的答案.我无法获得批准的答案,以便在我的设置中尽早返回结果,但这非常有效.
if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
//DO Portrait
}else{
//DO Landscape
}
Run Code Online (Sandbox Code Playgroud)
Shi*_*rin 11
要添加到已经回答的问题中:
您使用[[UIDevice currentDevice] orientation]哪个将产生以下值之一:
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
Run Code Online (Sandbox Code Playgroud)
文档可以在这里找到- (方向)和这里 - (UIDeviceOrientation).
(我不是故意要求使用前者,但这些信息对于评论来说非常重要.)
小智 7
试试这个:
[[UIApplication sharedApplication] statusBarOrientation]
Run Code Online (Sandbox Code Playgroud)
或者在Swift 3中:
UIApplication.shared.statusBarOrientation
Run Code Online (Sandbox Code Playgroud)
要专门检查特定方向,您还可以尝试这样的isLandscape或isPortrait属性:
UIApplication.shared.statusBarOrientation.isLandscape
Run Code Online (Sandbox Code Playgroud)
问题[[UIDevice currentDevice] orientation]是,它也将返回UIInterfaceOrientationUnknown哪个statusBarOrientation没有.
还有一个UIViewController属性,interfaceOrientation但在iOS 8中已弃用,因此不推荐使用.
你在这里查看文档statusBarOrientation
如果您遇到困难并且不断从UIDevice获取UIDeviceOrientationUnknown,您还可以使用UIViewController类的interfaceOrientation属性.
有一个很好的总结,为什么[[UIDevice currentdevice]方向]有时会失败:http://bynomial.com/blog/?p = 25,特别是如果你想快速检测方向(例如,如果你想当应用程序退出后台时立即检查).
小智 6
它可以帮助你......
-(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");
}
}
Run Code Online (Sandbox Code Playgroud)
作为 Swift 4.2 的简单解决方案
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
switch UIDevice.current.orientation{
case .portrait:
print("Portrait")
case .portraitUpsideDown:
print("PortraitUpsideDown")
case .landscapeLeft:
print("LandscapeLeft")
case .landscapeRight:
print("LandscapeRight")
default:
print("Another")
}
}
Run Code Online (Sandbox Code Playgroud)