CAShapeLayer的框架和界限 [英] Frame and Bounds of CAShapeLayer
问题描述
我正在处理 CAShapeLayer
。并尝试绘制非线性路径。我想将帧设置为 CAShapeLayer
。因此,我可以使用 CGPathGetPathBoundingBox
方法从 CGPathRef
获取帧。
I am working on CAShapeLayer
.And trying to draw non-linear path.I want to set frame to CAShapeLayer
.So i can use CGPathGetPathBoundingBox
method to get frame from CGPathRef
.
以下是代码:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100, (0), (M_PI_2), NO);
CGPathAddArc(path, NULL, rect.size.width/2, rect.size.height/2, 100-50, (M_PI_2), (0), YES);
CGPathCloseSubpath(path);
CAShapeLayer* arcLayer = [[CAShapeLayer alloc]init];
arcLayer.path = path;
arcLayer.frame = CGPathGetPathBoundingBox(path);
arcLayer.bounds = CGPathGetPathBoundingBox(path);
[self.layer addSublayer:arcLayer]; `
请仔细参考我的代码。我已经设置了相同的框架和界限到CAShapeLayer。我的问题是如果我没有设置边界(与框架相同),然后它不会显示我的内容或它不会在框架内显示我的内容。为什么?请帮助我。谢谢你。
Please refer my code carefully .I have set same frame and bounds to CAShapeLayer.My problem is if i am not setting bounds (same as frame) ,then it wont show my content or it wont show my content within frame.Why?Please help me.Thanking you.
推荐答案
当您修改 CALayer
的框架
时,您正在修改其尺寸并在其超级层的坐标空间中定位。 frame
是一个计算值,基于对象的界限
和位置
。在这种情况下,该层还没有超层,因此其坐标空间是无量纲的。设置框架
时,大小会传递到边界
属性,但由于无量纲空间中的任何点为零,位置将保持为零而不是您想要的位置。
When you modify the frame
of a CALayer
, you are modifying its size and position in the coordinate space of its superlayer. frame
is a computed value, based on the object's bounds
and position
. In this case, the layer does not yet have a superlayer so its coordinate space is dimensionless. When you set the frame
, the size gets carried through to the bounds
property, but since any point in a dimensionless space is zero, the position will remain zero instead of what you'd intended.
要解决此问题,您可以设置边界
在超级层的坐标空间中设置图层的大小和位置
(这是一个真正的非计算属性),然后再将其添加为子图层。
To solve this, you can set the bounds
to set the layer's size, and position
(which is a real non-computed property) in the coordinate space of the superlayer before adding it as a sublayer.
这篇关于CAShapeLayer的框架和界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!