使用此处的信息:iPhone Landscape-Only Utility-Template Application我只能以横向方式启动、使用和维护我的 View 。但是,我对轴感到困惑。
绝对轴的行为符合预期,意味着 (0,0) 是左上角,(240,0) 是顶部中心,(0,320) 是左下角等。但是,当我尝试进行计算时与我正在绘制的 View 相关,我发现 x,y 的方向就像肖像左上角是原点一样。因此,要在 View Controller 的中心点绘制一些内容,我需要执行以下操作:
CGPoint center = CGPointMake(self.view.center.y, self.view.center.x);
我认为这是因为我的 Controller self.view 引用的 UIView 给出了相对于其封闭框架的值,这意味着在纵向模式下该窗口的轴原点位于左上角。
是否有一些简单的方法可以解决我所缺少的问题?
文档表明转换属性正是我正在寻找的,但是,我在这里遇到了进一步的困惑。涉及 3 个基本属性:
- 框架
- 界限
- 居中
如果我在 viewDidLoad 中执行此操作:
// calculate new center point
CGFloat x = self.view.bounds.size.width / 2.0;
CGFloat y = self.view.bounds.size.height / 2.0;
CGPoint center = CGPointMake(y, x);
// set the new center point
self.view.center = center;
// Rotate the view 90 degrees counter clockwise around the new center point.
CGAffineTransform transform = self.view.transform;
transform = CGAffineTransformRotate(transform, -(M_PI / 2.0));
self.view.transform = transform;
现在根据引用文档,如果变换没有设置为恒等变换 self.view.frame 是未定义的。所以我应该使用边界和中心。
self.view.center 现在是正确的,因为我将其设置为我想要的值。 self.view.bounds 看起来没有变化。 self.view.frame 似乎正是我想要的,但如上所述,引用文献声称它无效。
因此,虽然我可以获得我认为正确的数字,但我担心我忽略了一些关键的东西,这些东西以后会变得很麻烦。
谢谢。
最佳答案
对于 Quartz 2D,坐标系的原点 (0,0) 位于图形的左下角,而不是左上角。也许这就是你所缺少的。
关于iPhone 仅横向应用程序 View 轴困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/738216/