检测iOS设备方向锁定

发布于 2024-10-26 01:45:53 字数 131 浏览 3 评论 0原文

如何以编程方式检查 iOS 中设备方向是否已锁定?我在 UIDevice 中看不到任何内容。我知道这对应用程序来说应该是透明的。但如果方向被锁定,我想更改显示内容的方式(就像 Youtube 应用程序那样;它锁定为横向而不是纵向)。这一定是可能的。

How can I programmatically check whether the device orientation is locked in iOS? I see nothing in UIDevice. I know that this is supposed to be transparent to the app. But I'd like to change the way I display content if the orientation is locked (as the Youtube app does; it locks to landscape rather than portrait). This must be possible.

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

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

发布评论

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

评论(3

浊酒尽余欢 2024-11-02 01:45:53

您可以使用 UIAccelerometer 类来详细确定手机的方向。如果加速度矢量进入其最大绝对分量在 X 轴上的状态,即横向。理论上,这可以用于检测方向锁定:如果在发生这种情况的几秒钟内,控制器没有收到对 shouldRotateToInterfaceOrientation 的调用,并且 [[UIDevice currentDevice] 方向] 属性不在横向,那么您可以安全地假设用户已锁定方向。

这很复杂,并且有延迟,因为 shouldRotateToInterfaceOrientation 将在实际矢量实际进入景观区域之后调用。整个想法有点黑客,你可能应该重新考虑为什么当用户偏好不显示景观视图时你真的需要呈现它们。

You can use the UIAccelerometer class to determine the phone's orientation in great detail. If the acceleration vector enters a state where its largest absolute component is on the X axis, that is a landscape orientation. In theory this could be used to detect the orientation lock: if, within a few seconds of this happening, the controller does NOT receive a call to shouldRotateToInterfaceOrientation, and the [[UIDevice currentDevice] orientation] property is not in landscape, you could then safely assume the user has the orientation locked.

This complicated, and has a delay because shouldRotateToInterfaceOrientation will be called well after the actual vector has actually entered a landscape region. The whole idea is a bit of a hack, and you should probably reconsider why you really need to present landscape views when the user has a preference not to show them.

遮了一弯 2024-11-02 01:45:53

无法检测方向是否已锁定。 YouTube 应用程序不会锁定横向,它只是以横向方向显示电影,但是当您旋转 iPhone 时,电影也会旋转(如果没有方向锁定)。

IOW 方向锁定由系统处理,对您的应用程序透明。

如果您想实现此功能 - 即使 iPhone 处于纵向模式,也只需以横向模式显示您的视图,然后启用视图旋转。其行为方式与 YouTube 应用程序相同。

评论更新:

.h

BOOL rotationEnabled;

.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  return rotationEnabled || ( toInterfaceOrientation == UIInterfaceOrientationLandscapeRight );
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  rotationEnabled = YES;
}

There's no way how to detect if orientation is locked or not. YouTube app doesn't lock to landscape, it just displays movie in landscape orientation, but when you rotate your iPhone, movie rotates as well (if there's no orientation lock).

IOW orientation lock is handled by system, transparent to your application.

If you want to achieve this functionality - just display your view in landscape mode even if the iPhone is in portrait mode and later enable rotation of your view. It will behave in the same way as YouTube app.

Update for comment:

.h

BOOL rotationEnabled;

.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  return rotationEnabled || ( toInterfaceOrientation == UIInterfaceOrientationLandscapeRight );
}

- (void)viewDidAppear:(BOOL)animated {
  [super viewDidAppear:animated];
  rotationEnabled = YES;
}
幽梦紫曦~ 2024-11-02 01:45:53

我认为你无法知道方向是否被锁定。我前段时间找过这个,但什么也没找到。您可以做的就是忽略方向并仅在视图控制器中提供横向...然后无论如何它都会以横向显示。

我想这也是 youtube 正在做的事情。

I don't think you can find out if the orientation is locked. I have looked for that some time ago and found nothing. What you can do is ignoring the orientation and just offer landscape in your view controller... then it will be shown in landscape no matter what.

I think this is what youtube is doing as well.

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