【问题标题】:How to Size UIView from its CALayers?如何从它的 CALayers 中调整 UIView 的大小?
【发布时间】:2011-12-30 03:05:58
【问题描述】:

我有一个具有 CALayers 层次结构的 UIView。

我正在使用图像创建图层,所以在创建图层之前我不知道 UIView 的最终大小。

如何找出允许触摸所有层的最终 UIView 大小?

图层可能是矩形的,然后旋转,这样它会更突出,这一事实也很复杂。由于它的仿射变换,我无法使用图层的帧大小。

我确实有一个解决方案,可以让一个 UIView 成为屏幕大小,我可以将我所有的 CALayers 添加到这个 UIView,但是我将有几组这些层层次结构,我希望它们位于单独的 UIView 中,所以我可以使用 UIView 手势来平移/缩放/旋转。

【问题讨论】:

    标签: iphone calayer


    【解决方案1】:

    您可以为此使用CGMutablePathRef。然后遍历view.layer.sublayers中的所有CALayer-s:

    CGPathAddRect(path,
                  &transform,
                  CGRectOffset(i.bounds,
                               -CGRectGetMidX(i.bounds), 
                               -CGRectGetMidY(i.bounds)));
    

    矩形被偏移以使其中心位于CGPointZerotransform 这里是图层(或缩放)的旋转加上图层位置的平移。即:

    CGAffineTransform transform =
        CGAffineTransformConcat(CATransform3DGetAffineTransform(i.transform),
                                CGAffineTransformMakeTranslation(i.position.x,
                                                                 i.position.y));
    

    最后:

    view.bounds = CGPathGetBoundingBox(path);
    

    现在边界最小并且包含所有层。

    【讨论】:

    • 谢谢 - 我从来没有发现过。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    相关资源
    最近更新 更多