Is there a way to convert the coordinate system after a change to Landscape Orientation?

拈花ヽ惹草 提交于 2019-12-04 09:39:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/896994/is-there-a-way-to-convert-the-coordinate-system-after-a-change-to-landscape-orie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!