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
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
CGLayeris 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.