iPhone正确的横向窗口坐标
我尝试使用以下代码获取表视图的窗口坐标:iPhone正确的横向窗口坐标,iphone,window,coordinates,landscape,Iphone,Window,Coordinates,Landscape,我尝试使用以下代码获取表视图的窗口坐标: [self.tableView.superview转换器rect:self.tableView.frame-to-view:nil] 它在纵向模式下报告正确的坐标,但当我旋转到横向时,它不再报告正确的坐标。首先,它翻转x、y坐标以及宽度和高度。但这并不是真正的问题。真正的问题是坐标不正确。在纵向视图中,表视图框架的窗口坐标为{0,114},{320,322},而在横向视图中,窗口坐标为{32,0},{204,480}。显然这里的x值是不正确的,对吗?应该
[self.tableView.superview转换器rect:self.tableView.frame-to-view:nil]
它在纵向模式下报告正确的坐标,但当我旋转到横向时,它不再报告正确的坐标。首先,它翻转x、y坐标以及宽度和高度。但这并不是真正的问题。真正的问题是坐标不正确。在纵向视图中,表视图框架的窗口坐标为{0,114},{320,322},而在横向视图中,窗口坐标为{32,0},{204,480}。显然这里的x值是不正确的,对吗?应该是84吗?我正在寻找解决此问题的方法,如果有人知道如何在横向模式下获得视图的正确窗口坐标,如果您能与我分享这些知识,我将不胜感激
以下是一些屏幕截图,您可以查看视图布局
肖像:
横向:您应该能够从self.tableView.bounds获取当前的表视图坐标您的代码应该是:
[tableView convertRect:tableView.bounds toView:[UIApplication sharedApplication].keyWindow];
这将为您提供窗口坐标系中视图的矩形。确保使用“边界”而不是“框架”。框架是视图在其父视图坐标系中的矩形。“边界”是其自身系统中的视图矩形。因此,上面的代码要求表视图将自己的矩形从自己的系统转换为窗口的系统。您以前的代码要求表的父视图将表的矩形从父坐标系转换为零 我找到了我认为是解决方案的开端。您和我看到的坐标似乎基于左下角或右上角,这取决于方向是UIInterfaceOrientationAndscapeRight还是UIInterfaceOrientationAndscapeLeft 我还不知道为什么,但希望这能有所帮助。:) [更新] 所以我猜在正常的纵向模式下,窗口的原点是0,0,并且随着ipad/iphone旋转 我就是这样解决的 首先,我获取我的方向、窗口边界和窗口内视图的矩形(坐标不稳定) 如果方向是横向的,我反转x和y坐标以及宽度和高度
if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) {
windowRect = XYWidthHeightRectSwap(windowRect);
viewRectAbsolute = XYWidthHeightRectSwap(viewRectAbsolute);
}
然后我调用我的函数来固定原点,无论ipad/iphone如何旋转,它都是基于左上角的。
它根据0,0当前所在的位置(取决于方向)固定原点
下面是我使用的两个函数
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;
}
尝试边界而不是帧
self.parentViewController.view.bounds
因为它让我根据当前的方向调整坐标这是一种技巧,但它对我有效:
viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height);
UIView *toView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
[self.tableView convertRect:self.tableView.bounds toView:toView];
我不确定这是不是最好的解决办法。如果根视图控制器不支持与当前视图控制器相同的方向,则它可能无法可靠工作 实际上,convertRect:toView:的文档中说,“如果视图为nil,则此方法将转换为窗口基准坐标。”我尝试了您的代码,它给出了与我相同的结果。谢谢你的建议。我也看到了同样的问题。有时它能正确计算坐标,有时它不能。
UIView *toView = [UIApplication sharedApplication].keyWindow.rootViewController.view;
[self.tableView convertRect:self.tableView.bounds toView:toView];