如何处理UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown?

发布于 2024-10-07 03:04:49 字数 607 浏览 0 评论 0 原文

这是一个菜鸟问题。

我使用以下方法检测方向:

UIInterfaceOrientationorientation = [[UIDevice currentDevice]orientation];

一切都很好,我根据报告的方向重新定位我的文本字段和标签

if (orientation = = UIDeviceOrientationPortrait ||orientation == UIDeviceOrientationPortraitUpsideDown)

和 else{} 对于其他一切,

我最近才发现的问题是 UIDevice 报告 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown。我该如何处理这种情况?我如何知道 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown 是在纵向还是横向中发生?我知道设备朝上或朝下,但我不知道是否应该将所有内容重新定位为纵向或横向。

谢谢你!

a noob question here.

i detect the orientation with:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

all is fine and dandy and I reposition my text fields and labels according to the reported orientation with

if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)

and else{} for everything else

the problem that i only recently discovered is when the UIDevice reports UIDeviceOrientationFaceUp or UIDeviceOrientationFaceDown. how do I deal with this situation ? how do I know whether UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown is happening in Portrait or Landscape ? I know that the device is facing up or down, but I don't know if I should reposition everything to Portrait or Landscape.

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奶气 2024-10-14 03:04:49

Apple 建议不要使用设备方向进行视图布局。相反,每个视图控制器都有一个 interfaceOrientation 属性,并且 UIApplication 有一个 statusBarOrientation 属性,这两个属性都会返回当前界面< /em> 方向,适合视图布局。

要监视更改,有 UIViewController 方法,例如 willRotateToInterfaceOrientation:duration: 将被调用,并发出诸如 UIApplicationWillChangeStatusBarOrientationNotification 将在界面方向发生变化。

Apple recommends against using device orientation for view layout. Instead, each view controller has an interfaceOrientation property, and UIApplication has a statusBarOrientation property, both of which will return the current interface orientation, which is suitable for view layout.

To monitor for changes, there are UIViewController methods like willRotateToInterfaceOrientation:duration: that will be called and notifications such as UIApplicationWillChangeStatusBarOrientationNotification that will be posted when an interface orientation change occurs.

深海夜未眠 2024-10-14 03:04:49

有点晚了,希望对某人有所帮助。

对于 iOS 6.0 及更高版本:

1) 在视图设置期间设置此代码以接收旋转通知:

  // Set Listener to receive orientation notifications from the device
  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(detectOrientation)
                                               name:UIDeviceOrientationDidChangeNotification
                                             object:nil];

2) 设置此代码以在旋转后捕获通知:

-(void)detectOrientation{      
  switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationFaceDown:{
      NSLog(@"UIDeviceOrientationFaceDown");
      break;
    }
    case UIDeviceOrientationFaceUp:{
      NSLog(@"UIDeviceOrientationFaceUp");
      break;
    }
    case UIDeviceOrientationLandscapeLeft:{
      NSLog(@"UIDeviceOrientationLandscapeLeft");
      break;
    }
    case UIDeviceOrientationLandscapeRight:{
      NSLog(@"UIDeviceOrientationLandscapeRight");
      break;
    }
    case UIDeviceOrientationPortrait:{
      NSLog(@"UIDeviceOrientationPortrait");
      break;
    }
    case UIDeviceOrientationPortraitUpsideDown:{
      NSLog(@"UIDeviceOrientationPortraitUpsideDown");
      break;
    }
    case UIDeviceOrientationUnknown:{
      NSLog(@"UIDeviceOrientationUnknown");
      break;
    }
    default:{
      break;
    }
  }

}

A bit late, hopefully it would help some one.

For iOS 6.0 and later:

1) Set this code during your View Setup to receive rotation notification:

  // Set Listener to receive orientation notifications from the device
  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(detectOrientation)
                                               name:UIDeviceOrientationDidChangeNotification
                                             object:nil];

2) Set this code to catch the Notification after rotation:

-(void)detectOrientation{      
  switch ([[UIDevice currentDevice] orientation]) {
    case UIDeviceOrientationFaceDown:{
      NSLog(@"UIDeviceOrientationFaceDown");
      break;
    }
    case UIDeviceOrientationFaceUp:{
      NSLog(@"UIDeviceOrientationFaceUp");
      break;
    }
    case UIDeviceOrientationLandscapeLeft:{
      NSLog(@"UIDeviceOrientationLandscapeLeft");
      break;
    }
    case UIDeviceOrientationLandscapeRight:{
      NSLog(@"UIDeviceOrientationLandscapeRight");
      break;
    }
    case UIDeviceOrientationPortrait:{
      NSLog(@"UIDeviceOrientationPortrait");
      break;
    }
    case UIDeviceOrientationPortraitUpsideDown:{
      NSLog(@"UIDeviceOrientationPortraitUpsideDown");
      break;
    }
    case UIDeviceOrientationUnknown:{
      NSLog(@"UIDeviceOrientationUnknown");
      break;
    }
    default:{
      break;
    }
  }

}
撩起发的微风 2024-10-14 03:04:49

我有同样的问题

[[UIDevice currentDevice] beginGenerateDeviceOrientationNotifications];

此方法生成 7 种可能的设备位置状态的通知:

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown

但我只需要前 4 个,这是我解决此问题的方法(包含代码):
如何处理 UIDeviceOrientation 来管理视图布局

I have the same problems with

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

This method generate notifications for 7 possible device positional states:

UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown

But I only need the first 4, well this is my approach to solve this problem (with code included):
How to handle UIDeviceOrientation for manage views layouts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文