EJV*_*EJV 8 iphone landscape window coordinates
我想使用以下代码获取表视图的窗口坐标:
[self.tableView.superview convertRect:self.tableView.frame toView:nil]
它在纵向模式下报告正确的坐标,但是当我旋转到横向时,它不再报告正确的坐标.首先,它会翻转x,y坐标以及宽度和高度.但这不是真正的问题.真正的问题是坐标不正确.在纵向中,表格视图框架的窗口坐标为{{0,114},{320,322}},而在横向中,窗口坐标为{{32,0},{204,480}}.显然这里的x值是不正确的,对吧?不应该是84吗?我正在寻找一个解决这个问题的方法,如果有人知道如何在横向模式下获得视图的正确窗口坐标,如果你能与我分享这些知识,我将非常感激.
以下是一些屏幕截图,以便您可以看到视图布局.
我发现了我认为是解决方案的开端.您和我看到的坐标似乎基于左下角或右上角,具体取决于方向是UIInterfaceOrientationLandscapeRight还是UIInterfaceOrientationLandscapeLeft.
我不知道为什么,但希望这会有所帮助.:)
[更新]所以我猜在正常肖像模式下窗口的原点是0,0,并随着ipad/iphone旋转.
所以这就是我解决这个问题的方法.
首先,我在窗口内抓住我的方向,窗口边界和视图的矩形(带有不稳定的坐标)
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGRect windowRect = appDelegate.window.bounds;
CGRect viewRectAbsolute = [self.guestEntryTableView convertRect:self.guestEntryTableView.bounds toView:nil];
Run Code Online (Sandbox Code Playgroud)
然后,如果方向是横向,我将反转x和y坐标以及宽度和高度
if (UIInterfaceOrientationLandscapeLeft == orientation ||UIInterfaceOrientationLandscapeRight == orientation ) {
windowRect = XYWidthHeightRectSwap(windowRect);
viewRectAbsolute = XYWidthHeightRectSwap(viewRectAbsolute);
}
Run Code Online (Sandbox Code Playgroud)
然后,无论ipad/iphone是否旋转,我都会调用我的功能来固定原点为左上角.它根据0,0当前居住的位置(取决于方向)修复原点
viewRectAbsolute = FixOriginRotation(viewRectAbsolute, orientation, windowRect.size.width, windowRect.size.height);
Run Code Online (Sandbox Code Playgroud)
这是我使用的两个功能
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5341 次 |
| 最近记录: |