【发布时间】:2015-06-06 17:15:54
【问题描述】:
我在UIScrollView 中有一个UIView,并且正在使用带有几个子层的CAShapeLayer 在视图中绘制。在某些情况下,图层不可见,我想滚动视图以使图层可见。要滚动我正在使用:
[self.scrollView setContentOffset: CGPointMake(0, offset) animated: NO];
我很难弄清楚偏移量是多少。我试图得到封闭的矩形,但它的原点总是在 (0,0)。
如何计算要在偏移中使用的图层的位置?
更新:
这似乎可以为所有子层获取封闭的矩形:
- (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;
}
如果有更好、更简单的方法,请随时发表评论。
【问题讨论】:
标签: ios objective-c uiview uiscrollview cashapelayer