6

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?

RexOnRoids
  • 14,002
  • 33
  • 96
  • 136

1 Answers1

10

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
  • 170,088
  • 45
  • 397
  • 571
  • What if the coordinates are of a camera View Picture like CIRectangleFeature or the CGPoints of area of interest in Image. Now if the image is in landscape and being converted to portrait with AspectFit and need to show the area of interest. How would we convert the Points pointing toward landscape to portrait version of app? In my case i converted the camera image points to View coordinates but they are in landscape version – Hassy May 23 '17 at 10:00