2010-10-19 70 views
1

我正在嘗試確定當前在舞臺上呈現的可見矩形的全局座標。獲取組件可見區域的全局座標

具體而言,如果畫布具有明確的高度和寬度,並且是具有滾動條的面板的子項,則滾動條可以「隱藏」畫布的一部分。 ContentToGlobal(x,y)提供當時內容的全局位置,但內容座標可以滾動父面板的邊界,並繼續給出不可見的x,y座標。

有沒有一種方法可以確定未被隱藏的可視矩形?

回答

4

事實證明,UIComponent類有一個未公開的公共函數,它正是我一直在尋找:

private function getVisibleRectangle(container:Container, child:UIComponent):Rectangle 
    { 
     var rect:Rectangle = child.getBounds(child.stage); 
     var containerMetrics:EdgeMetrics = container.viewMetrics; 
     var containerPoint:Point = container.localToGlobal(new Point(0, 0)); 
     var containerRect:Rectangle = new Rectangle(containerPoint.x + containerMetrics.left, 
      containerPoint.y + containerMetrics.top, 
      container.width - containerMetrics.left - containerMetrics.right, 
      container.height - containerMetrics.top - containerMetrics.bottom); 

     if (rect.left >= containerRect.right || 
      rect.right <= containerRect.left || 
      rect.top >= containerRect.bottom || 
      rect.bottom <= containerRect.top) 
      return null; 

     rect.left = Math.max(rect.left, containerRect.left); 
     rect.right = Math.min(rect.right, containerRect.right); 
     rect.top = Math.max(rect.top, containerRect.top); 
     rect.bottom = Math.min(rect.bottom, containerRect.bottom); 
     return rect; 
    } 

使用示例。

/** 
* @private 
* 
* Get the bounds of this object that are visible to the user 
* on the screen. 
* 
* @param targetParent The parent to stop at when calculating the visible 
* bounds. If null, this object's system manager will be used as 
* the parent. 
* 
* @return a <code>Rectangle</code> including the visible portion of the this 
* object. The rectangle is in global coordinates. 
*/ 
public function getVisibleRect(targetParent:DisplayObject = null):Rectangle 

這是從類牽引,被記錄爲私有,但實際上是可以在任何UI對象上使用可公開訪問的功能。

由於這不在文檔anywehere,我想它可能會受到未來的變化。

1

不,沒有簡單的解決方案。

您應該計算手動可見矩形:

<mx:Panel id="panel" width="200" height="200"> 
    <mx:Canvas backgroundColor="green" width="100" height="100"/> 
    <mx:Canvas id="canvas" backgroundColor="red" width="500" height="400" 
     enterFrame="trace(getVisibleRectangle(panel, canvas));"/> 
</mx:Panel>