即使在设备方向切换后,如何保持 CGLayerRef 恒定?
我正在使用 CoreGraphics 将 CGLayerRef 存储到变量中。
我将路径绘制到屏幕外的 CGLayerRef 中,然后在需要时渲染它。这非常有用,因为我绘制了很多路径,这有助于提高性能。
当 iPad 的方向发生变化时,就会出现此问题。发生这种情况时,我需要将我的绘图保留在 CGLayerRef 中而不进行任何更改,尽管框架已更改。我不想重画其中的所有内容,因为它会非常慢。
那么...我将如何保持它在之前的方向呢?
用图片总结一下:
这是我正常的纵向:
当方向切换时,我的目标是它看起来像这样:
提前致谢,
-David
I am using CoreGraphics to have a CGLayerRef that I store into a variable.
I draw paths into the CGLayerRef offscreen and then when I need, I render it. This works great because I am drawing lots of paths and this helps with the performance.
The problem occurs when the iPad's orientation changes. When this happens I need to keep my my drawings in the CGLayerRef without any changes, despite the fact that the frame changed. I don't want to redraw everything inside of it, because it will be very slow.
So... how will I keep it as it was in the previous orientation?
To sum it up in pictures:
This is my normal portrait orientation:
And when the orientation switches my goal is that it will look something like this:
Thanks in advance,
-David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想要一个视图始终以相对于设备的相同方向绘制其内容,而不管界面(状态栏等)的方向如何。因此,在您的图像中,如果“hello”是通过物理音量按钮与“h”一起绘制的,那么您总是希望由物理音量按钮绘制“h”,即使用户旋转了设备(以及其余部分) UI 已旋转)。
iOS 通过设置根视图控制器视图的变换来旋转用户界面。您可以在调试器中检查这一点:
因此,您可以在绘制内容之前通过将逆变换应用于图形上下文来撤消此操作。但是您也必须调整图形上下文的原点,这使得它有点棘手。
首先,您需要将原点移动到视图的中心。然后应用逆变换来撤消界面旋转。然后将原点移回原点。假设您的
CGLayer
与您的视图大小相同(纵向),这应该可行:它并不完美。正如我所说,它最终会绘制:始终相对于设备处于相同的方向。但是,由于界面旋转的工作方式,当用户旋转设备时,它会重新绘制旋转 90 度的视图,并将视图旋转到正确的方向。
我在这里放置了一个测试项目:
https://github.com/mayoff/stackoverflow-cglayer-unrotating
在测试项目中,您可以看到一个正常运行的半透明工具栏 - 它旋转到最接近地面的屏幕边缘。下面是使用上面的
drawRect:
方法来补偿用户界面旋转的视图。It sounds like you want a view that always draws its contents in the same orientation relative to the device, regardless of the orientation of the interface (status bar, etc.). So in your images, if “hello” is drawn with the “h” by the physical volume buttons, then you always want the “h” drawn by the physical volume buttons, even if the user has rotated the device (and the rest of the UI has rotated).
iOS rotates the user interface by setting the transform of your root view controller's view. You can check this in the debugger:
So you can undo this by applying the inverse transform to your graphics context before drawing your content. But you have to adjust the origin of your graphics context too, which makes it a bit trickier.
First, you need to move the origin to the center of the view. Then you apply the inverse transform to undo the interface rotation. Then you move the origin back. Assuming your
CGLayer
is the same size as your view (in portrait orientation), this should work:It's not perfect. It ends up drawing as I said: always in the same orientation relative to the device. However, because of the way interface rotation works, when the user rotates the device, it redraws the view rotated 90 degrees, and the rotates the view into the correct orientation.
I have put a test project here:
https://github.com/mayoff/stackoverflow-cglayer-unrotating
In the test project, you can see a translucent toolbar that acts normally - it rotates to the edge of the screen nearest the ground. Under that is the view that uses the
drawRect:
method above to compensate for the user interface rotation.如果您不希望更改界面方向,则只需在视图控制器中注释掉 shouldAutorotateToInterfaceOrientation 方法即可。
If you don't want the interface orientation to change then just comment out the shouldAutorotateToInterfaceOrientation method in your view controller.