【问题标题】:Scroll a CAShapeLayer into view将 CAShapeLayer 滚动到视图中
【发布时间】: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


    【解决方案1】:

    使用convertRect:toView: 方法将框架转换为滚动视图的坐标系,生成的矩形应为您提供所需的内容偏移量。

    【讨论】:

    • 谢谢,我会试试的。但问题仍然存在,如何获得相对于视图的层的封闭矩形?
    • 查看我上面的更新,convertRect:toView: 不需要这种方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2014-02-24
    • 2012-07-19
    • 2015-04-29
    • 2016-11-01
    • 2016-04-07
    相关资源
    最近更新 更多