本文介绍了iPhone正确的风景窗口坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码获取表格视图的窗口坐标:

I am trying to get the window coordinates of a table view using the following code:

[self.tableView.superview convertRect:self.tableView.frame toView:nil]

在纵向模式下,它报告正确的坐标,但是当我旋转到横向时,它不再报告正确的坐标.首先,它翻转x,y坐标以及宽度和高度.但这并不是真正的问题.真正的问题是坐标不正确.在纵向模式下,表格视图框架的窗口坐标为{{0,114},{320,322}},而在横向模式下,窗口坐标为{{32,0},{204,480}}.显然这里的x值不正确,对吧?不应该是84吗?我正在寻找解决此问题的方法,如果有人知道如何在横向模式下获取视图的正确窗口坐标,那么如果您愿意与我分享这一知识,我将不胜感激.

It reports the correct coordinates while in portrait mode, but when I rotate to landscape it no longer reports correct coordinates. First off, it flips the x, y coordinates and the width and height. That's not really the problem though. The real problem is that the coordinates are incorrect. In portrait the window coordinates for the table view's frame are {{0, 114}, {320, 322}}, while in landscape the window coordinates are {{32, 0}, {204, 480}}. Obviously the x-value here is incorrect, right? Shouldn't it be 84? I'm looking for a fix to this problem, and if anybody knows how to get the correct window coordinates of a view in landscape mode, I would greatly appreciate it if you would share that knowledge with me.

以下是一些屏幕截图,因此您可以查看视图布局.

Here are some screenshots so you can see the view layout.

肖像: http://i.stack.imgur.com/IaKJc.png

风景: http://i.stack.imgur.com/JHUV6.png

推荐答案

我发现我认为这是解决方案的开始.您和我看到的坐标似乎基于左下角或右上角,这取决于方向是UIInterfaceOrientationLandscapeRight还是UIInterfaceOrientationLandscapeLeft.

I've found what I believe to be the beginnings of the solution. It seems the coordinates you and I are seeing are being based on the bottom left or top right, depending on whether the orientation is UIInterfaceOrientationLandscapeRight or UIInterfaceOrientationLandscapeLeft.

我还不知道为什么,但是希望能有所帮助. :)

I don't know why yet, but hopefully that helps. :)

[更新]因此,我认为正常的人像模式下窗口的原点是0,0,并且会随着ipad/iphone旋转.

[UPDATE]So I guess the origin of the window is 0,0 in normal portrait mode, and rotates with the ipad/iphone.

这就是我解决这个问题的方式.

So here's how I solved this.

首先,我在窗口内(带有不固定的坐标)抓取方向,窗口边界和视图的矩形

First I grab my orientation, window bounds and the rect of my view within the window (with the wonky coordinates)

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect windowRect = appDelegate.window.bounds;
CGRect viewRectAbsolute = [self.guestEntryTableView convertRect:self.guestEntryTableView.bounds toView:nil];

然后,如果方向是横向,我将反转x和y坐标以及宽度和高度

Then if the orientation is landscape, I reverse the x and y coordinates and the width and height

if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) {
    windowRect = XYWidthHeightRectSwap(windowRect);
    viewRectAbsolute = XYWidthHeightRectSwap(viewRectAbsolute);
}

然后,无论ipad/iphone的旋转角度如何,我都将用于固定原点的函数称为基于左上角.它根据当前0,0的住处(取决于方向)来固定原点

Then I call my function for fixing the origin to be based on the top left no matter the rotation of the ipad/iphone.It fixes the origin depending on where 0,0 currently lives (depending on the orientation)

viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height);

这是我使用的两个功能

CGRect XYWidthHeightRectSwap(CGRect rect) {
    CGRect newRect;
    newRect.origin.x = rect.origin.y;
    newRect.origin.y = rect.origin.x;
    newRect.size.width = rect.size.height;
    newRect.size.height = rect.size.width;
    return newRect;
}

CGRect FixOriginRotation(CGRect rect, UIInterfaceOrientation orientation, int parentWidth, int parentHeight) {
    CGRect newRect;
    switch(orientation)
    {
        case UIInterfaceOrientationLandscapeLeft:
            newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), rect.origin.y, rect.size.width, rect.size.height);
            break;
        case UIInterfaceOrientationLandscapeRight:
            newRect = CGRectMake(rect.origin.x, parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
            break;
        case UIInterfaceOrientationPortrait:
            newRect = rect;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            newRect = CGRectMake(parentWidth - (rect.size.width + rect.origin.x), parentHeight - (rect.size.height + rect.origin.y), rect.size.width, rect.size.height);
            break;
    }
    return newRect;
}

这篇关于iPhone正确的风景窗口坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:41