2015-06-06 18 views
0

我在UIScrollView內部有一個UIView,並且我正在使用帶有幾個子圖層的CAShapeLayer在視圖中繪製。在某些情況下,圖層不可見,我想滾動視圖以便圖層變爲可見。滾動我正在使用:將CAShapeLayer滾動到視圖中

[self.scrollView setContentOffset: CGPointMake(0, offset) animated: NO]; 

我很難找出偏移量是多少。我試圖得到封閉的矩形,但始終有(0,0)的原點。

如何計算圖層在偏移量中的位置?

UPDATE:

這似乎是工作,以獲得包圍矩形爲所有子層:

- (CGRect) enclosingLayerRect 
{ 
    CGRect rect = CGRectZero; 

    if (self.sublayers.count) 
    { 
     CAShapeLayer *layer = self.sublayers[0]; // need to get the first one, otherwise the origin will be (0,0) 

     rect = CGPathGetBoundingBox(layer.path); 

     for (CAShapeLayer *layer in self.sublayers) 
     { 
      CGRect layerRect = CGPathGetBoundingBox(layer.path); 
      rect = CGRectUnion(rect, layerRect); 
     } 
    } 

    return rect; 
} 

隨意評論,如果有做這更好的,更簡單的方法。

回答

0

使用convertRect:toView:方法將框架轉換爲滾動視圖的座標系,並且生成的矩形應該爲您提供所需的內容偏移量。

+0

謝謝,我會嘗試。但問題仍然存在,我如何獲得相對於視圖的圖層的封閉矩形? – Koen

+0

看到我上面的更新,'convertRect:toView:'不是必要的那種方法。 – Koen

相關問題