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?
views:
662answers:
1
+4
A:
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.
Brad Larson
2009-05-22 12:18:21
You, are the man!
RexOnRoids
2009-05-22 13:17:29