In a view-based iPhone OS application, I change the orientation from the initial portrait orientation to a landscape orientation (UIInterfaceOrientationLandscapeRight). But now the x,y origin (0,0) is in the lower-left corner (instead of the usual upper-left) and each time I want to do something that involves coordinates I must re-calculate to compensate for the new coordinate system. I have also noticed that my views within the new coordinate system are not behaving normally at times. So, Is there a way to convert the coordinate system ONCE, right after I switch the orientation, so that I can keep thinking of my coordinates as having an origin in the upper-left corner?
If you do the recommended approach of applying a transform to your main view in order to rotate it:
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGAffineTransform transform = primaryView.transform;
// Use the status bar frame to determine the center point of the window's content area.
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
CGPoint center = CGPointMake(60.0, bounds.size.height / 2.0);
// Set the center point of the view to the center point of the window's content area.
primaryView.center = center;
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
primaryView.transform = transform;
}
then any subviews you add to this main view should use the standard coordinate system. The transform that is applied to the main view takes care of rotating the coordinates of the subviews, as well. This works well for me in my application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With