2011-05-17 75 views
8

我嘗試使用下面的代碼來獲得一個表視圖的窗口座標:iPhone正確景觀窗口座標

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

據報告正確的座標,而在人像模式,但是當我旋轉爲橫向它不再報告正確的座標。首先,它翻轉x,y座標以及寬度和高度。但這並不是真正的問題。真正的問題是座標不正確。在縱向上,表格視圖框架的窗口座標爲{{0,114},{320,322}},而在橫向上,窗口座標爲{{32,0},{204,480}}。顯然這裏的x值是不正確的,對吧?難道不是84?我正在尋找解決這個問題的方法,如果有人知道如何獲得正確的窗口橫向模式下的視圖座標,如果您願意與我分享這些知識,我將不勝感激。

這裏有一些截圖,所以你可以看到視圖佈局。

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

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

+0

我看到了同樣的問題。有時它正確地計算座標,有時它不會。 – 2011-07-19 06:22:36

回答

0

你應該能夠得到當前表視圖從self.tableView.bounds

座標
0

你的代碼應該是:

[tableView convertRect:tableView.bounds toView:[UIApplication sharedApplication].keyWindow]; 

這會在窗口的座標系中給出視圖的矩形。一定要使用「邊界」而不是「框架」。框架在其父視圖座標系中已經是視圖的矩形。 「邊界」是它自己系統中的視圖矩形。所以上面的代碼要求表視圖將它自己的矩形從它自己的系統轉換到窗口的系統。您之前的代碼是要求表的父視圖將表格的矩形從父座標系轉換爲無。

+0

實際上,convertRect:toView的文檔說:「如果視圖爲零,則此方法將轉換爲窗口底座座標。」我試過你的代碼,它和我的結果一樣。謝謝你的建議。 – EJV 2011-05-17 20:33:06

+0

啊。你是對的。 – Dancreek 2011-05-18 02:27:00

6

我發現我認爲是解決方案的開始。看起來您和我看到的座標是基於左下角還是右上角,取決於方向是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]; 

這時如果方向爲橫向,我扭轉X和Y座標和寬度和高度

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

然後,我打電話給我的功能固定的起源是基於左上方無論iPad/iPhone的旋轉。 它修復起源取決於其中,0,0目前居住(取決於方向)

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; 
} 
0

嘗試邊界,而不是框架 self.parentViewController.view.bounds 它根據當前的方向給我調整的座標

3

這是一個黑客,但它適用於我:

UIView *toView = [UIApplication sharedApplication].keyWindow.rootViewController.view; 
[self.tableView convertRect:self.tableView.bounds toView:toView]; 

我不確定這是最好的解決方案。如果您的根視圖控制器不支持與當前視圖控制器相同的方向,它可能無法可靠地工作。

+0

如果可以的話,我會給你這麼多upvotes。 – MusiGenesis 2014-04-01 05:35:42

+0

是它的黑客,但完美的作品。 – siuying 2014-12-16 07:48:58