iPhone 正确的横向窗口坐标
我正在尝试使用以下代码获取表格视图的窗口坐标:
[self.tableView.superview ConvertRect:self.tableView.frame toView:nil]
它在纵向模式下报告正确的坐标,但是当我旋转到横向时,它不再报告正确的坐标。首先,它翻转 x、y 坐标以及宽度和高度。但这并不是真正的问题。真正的问题是坐标不正确。在纵向中,表格视图框架的窗口坐标为 {{0, 114}, {320, 322}},而在横向中,窗口坐标为 {{32, 0}, {204, 480}}。显然这里的x值不正确,对吧?不应该是84吗?我正在寻找解决此问题的方法,如果有人知道如何在横向模式下获取视图的正确窗口坐标,如果您愿意与我分享这些知识,我将不胜感激。
以下是一些屏幕截图,以便您可以查看视图布局。
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]
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.
Portrait: https://i.sstatic.net/IaKJc.png
Landscape: https://i.sstatic.net/JHUV6.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经找到了我认为是解决方案的开端。看来你和我看到的坐标是基于左下角或右上角的,具体取决于方向是 UIInterfaceOrientationLandscapeRight 还是 UIInterfaceOrientationLandscapeLeft。
我还不知道为什么,但希望这会有所帮助。 :)
[更新]
所以我猜窗口的原点在正常纵向模式下是0,0,并随着ipad/iphone旋转。
这就是我解决这个问题的方法。
首先我抓住我的方向、窗口边界和窗口内视图的矩形(带有不稳定的坐标)
然后如果方向是横向的,我反转x和y坐标以及宽度和高度
然后我调用我的函数来修复无论 ipad/iphone 旋转如何,原点都基于左上角。
它根据 0,0 当前所在的位置固定原点(取决于方向)
这是我使用的两个函数
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. :)
[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)
Then if the orientation is landscape, I reverse the x and y coordinates and the width and height
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)
Here are the two functions I use
这是一个黑客,但它对我有用:
我不确定这是最好的解决方案。如果您的根视图控制器不支持与当前视图控制器相同的方向,它可能无法可靠地工作。
This is a hack, but it works for me:
I am not sure this is the best solution. It may not work reliably if your root view controller doesn't support the same orientations as the current view controller.
您应该能够从 self.tableView.bounds 获取当前表视图坐标
You should be able to get the current table view coordinates from self.tableView.bounds
您的代码应该是:
这将为您提供窗口坐标系中视图的矩形。请务必使用“边界”而不是“框架”。框架已经是视图在其父视图坐标系中的矩形。 “bounds”是其自身系统中的视图矩形。所以上面的代码要求表视图将自己的矩形从自己的系统转换为窗口的系统。您之前的代码要求表的父视图将表的矩形从父坐标系转换为空。
Your code should be:
That will give you the view's rectangle in the window's coordinate system. Be sure to use "bounds" and not "frame". frame is the rectangle of the view in its parent view coordinate system already. "bounds" is the view rectangle in its own system. So the above code asks the table view to convert its own rectangle from its own system to the window's system. Your previous code was asking the table's parent view to convert the table's rectangle from the parent coordinate system to nothing.
尝试使用边界而不是框架
self.parentViewController.view.bounds
因为它根据当前方向给了我调整后的坐标
Try bounds instead of frame
self.parentViewController.view.bounds
for it gives me adjusted coords according to the current orientation