旋转一些 UIView,但不是全部?
我正在做一个应用程序,其中有一个我不想自动旋转的 GLES 视图,以及其上的 UIKit 视图,确实需要自动旋转。
显然这不是你应该做的事情,因为我找不到任何好的文档。但我完全确定这正是我想要的。
我找到了一个解决方案,但感觉很老套:
- 创建窗口。
- 创建一些自动旋转的 UIViewController。
- 将他们的视图添加到窗口中。
- 创建一个不自动旋转的 OpenGL ES 视图控制器。
- 将其视图添加到窗口。
- 调用 BringSubviewToFront: 来获取自动旋转视图。
- 给自己一个高五分,同时努力忽略这种挥之不去的感觉,即这实际上是一个令人讨厌的黑客攻击。
有人知道更好的解决方案吗?注意:我绝对肯定我想自动旋转 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:
- Create window.
- Create some auto-rotated UIViewControllers.
- Add their views to the window.
- Create an OpenGL ES view controller that's not auto-rotated.
- Add its view to the window.
- Call bringSubviewToFront: for the auto-rotated views.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我针对类似情况提出了不同的策略。就我而言,我允许所有视图旋转,但然后使用变换将我想要保持稳定的视图恢复到其原始方向。这是可行的,因为“非旋转”视图是正方形的,但通过更多的数学运算,它也可以适用于其他形状。这是我在
willRotateToInterfaceOrientation:duration:
中放入的内容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:
The
steadyView
is s subview of the controller. This seems to do the trick.没有比您提议的更好的方法了。
您确定不只是想旋转 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.
在旋转时横向绘制 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:
andwillAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
to animate the content staying in place during the interface rotation.所以我了解了它在幕后是如何运作的; 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.