旋转一些 UIView,但不是全部?

发布于 2024-10-19 03:17:46 字数 471 浏览 0 评论 0原文

我正在做一个应用程序,其中有一个我不想自动旋转的 GLES 视图,以及其上的 UIKit 视图,确实需要自动旋转。

显然这不是你应该做的事情,因为我找不到任何好的文档。但我完全确定这正是我想要的。

我找到了一个解决方案,但感觉很老套:

  1. 创建窗口。
  2. 创建一些自动旋转的 UIViewController。
  3. 将他们的视图添加到窗口中。
  4. 创建一个不自动旋转的 OpenGL ES 视图控制器。
  5. 将其视图添加到窗口。
  6. 调用 BringSubviewToFront: 来获取自动旋转视图。
  7. 给自己一个高五分,同时努力忽略这种挥之不去的感觉,即这实际上是一个令人讨厌的黑客攻击。

有人知道更好的解决方案吗?注意:我绝对肯定我想自动旋转 UIKit 视图而不是 GL 视图。

感谢您的宝贵时间,非常感谢。 =)

I'm doing an app where I have a GLES view which I don't want to be auto-rotated, and UIKit views on top of that, which do need to be auto-rotated.

Apparently this is not something you're supposed to be doing, as I can't find any good documentation on it. However I'm totally sure this is exactly what I want.

I've found a solution, but it feels hacky:

  1. Create window.
  2. Create some auto-rotated UIViewControllers.
  3. Add their views to the window.
  4. Create an OpenGL ES view controller that's not auto-rotated.
  5. Add its view to the window.
  6. Call bringSubviewToFront: for the auto-rotated views.
  7. Give yourself a high-five, while trying to ignore the nagging feeling that this is actually a nasty hack.

Anyone know of a better solution? NB: I'm absolutely positive that I want to auto-rotate the UIKit views and not the GL view.

Thanks for your time, much appreciated. =)

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

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

发布评论

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

评论(4

作业与我同在 2024-10-26 03:17:46

我针对类似情况提出了不同的策略。就我而言,我允许所有视图旋转,但然后使用变换将我想要保持稳定的视图恢复到其原始方向。这是可行的,因为“非旋转”视图是正方形的,但通过更多的数学运算,它也可以适用于其他形状。这是我在 willRotateToInterfaceOrientation:duration: 中放入的内容

switch (toInterfaceOrientation) {
    case UIInterfaceOrientationPortrait:
        steadyView.transform = CGAffineTransformIdentity;
        break;
    case UIInterfaceOrientationLandscapeLeft:
        steadyView.transform = CGAffineTransformMakeRotation(M_PI_2);
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        steadyView.transform = CGAffineTransformMakeRotation(M_PI);
        break;
    case UIInterfaceOrientationLandscapeRight:
        steadyView.transform = CGAffineTransformMakeRotation(3*M_PI_2);
        break;
}

steadyView 是控制器的子视图。这似乎可以解决问题。

I've come up with a different strategy for a similar situation. In my case I allow all the views to rotate, but then use a transform to bring the view I want to keep steady back to its original orientation. This works because the "non-rotating" view is square, but with a bit more math it could work for other shapes as well. Here's what I put in my willRotateToInterfaceOrientation:duration:

switch (toInterfaceOrientation) {
    case UIInterfaceOrientationPortrait:
        steadyView.transform = CGAffineTransformIdentity;
        break;
    case UIInterfaceOrientationLandscapeLeft:
        steadyView.transform = CGAffineTransformMakeRotation(M_PI_2);
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        steadyView.transform = CGAffineTransformMakeRotation(M_PI);
        break;
    case UIInterfaceOrientationLandscapeRight:
        steadyView.transform = CGAffineTransformMakeRotation(3*M_PI_2);
        break;
}

The steadyView is s subview of the controller. This seems to do the trick.

伤感在游骋 2024-10-26 03:17:46

没有比您提议的更好的方法了。

您确定不只是想旋转 OpenGL 视图吗?如果您的应用程序仅限于 ES 2.0 设备和更高版本的 iOS 4.2,则对 OpenGL 视图使用 UIViewController 旋转不再有任何性能损失。

There isn't a good way to do this any better than what you proposed.

Are you sure you don't just want to rotate the OpenGL views too? If your application is limited to the ES 2.0 devices and later on iOS 4.2, there is no longer any performance penalty to using UIViewController rotation for OpenGL views.

鹿童谣 2024-10-26 03:17:46

在旋转时横向绘制 OpenGL 内容不是更简单吗?只需在渲染器中应用旋转即可撤消 OpenGL 视图的旋转。

您可能会看到无法接受的跳回原始轮换的情况,但您可以轻松解决该问题。视图完成旋转后,您可以设置返回旋转的动画。或者,您可以覆盖 willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration: 以在界面旋转期间为保持原位的内容设置动画。

Wouldn't it be simpler to just draw your OpenGL content sideways when rotated? Just apply a rotation in your renderer to undo the rotation of OpenGL view.

You may see an unacceptable jump back to the original rotation, but you could address that easily. You could animate the return rotation, once the view is finished rotating. Or you could override willAnimateFirstHalfOfRotationToInterfaceOrientation:duration: and willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration: to animate the content staying in place during the interface rotation.

白衬杉格子梦 2024-10-26 03:17:46

所以我了解了它在幕后是如何运作的; Frogblast 是对的,没有任何更好的方法可以做到这一点。

发生的情况是,当视图添加到没有子视图的窗口时,该视图的控制器自动成为“旋转权限”;如果该视图控制器从 shouldAutorotateToInterfaceOrientation 返回 NO,则其他视图也不会旋转。但是,添加到窗口的后续视图(带有控制器)仍然允许旋转,即使“旋转权限”确实旋转。

我不清楚他们为什么这样做,但你已经明白了。

So I found out how it works behind the scenes; Frogblast is right, there isn't any substantially better way to do it.

What happens is that when a view gets added to a window that has no subviews, that view's controller is automatically the "rotation authority"; If that view controller returns NO from shouldAutorotateToInterfaceOrientation, no other views will rotate either. However, subsequent views (with controllers) added to the window are still allowed not to rotate, even if the "rotation authority" does rotate.

Unclear to me why they've made it like this, but there you have it.

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