Ada*_*dam 16 calayer cashapelayer cgpath
简而言之:
frame
或bounds
为CAShapeLayer
(并且Apple未实现等效[UIView sizeThatFits]
)那么,什么是编程设定的新创建的框架的正确方法CAShapeLayer
与新添加的CGPath
?Apple的文档在此事上保持沉默.
我尝试过的东西,不起作用:
CAShapeLayer
CGPath
,将其添加到图层frame
- 它是{{0,0},{0,0}}
layer.frame = CGPathGetBoundingBox( layer.path )
帧现在是正确的,但路径现在是DOUBLE偏移 - 改变frame
路径有效地移动一个额外的(x,y)
像素
组: layer.bounds = CGPathGetBoundingBox( layer.path )
layer.position = CGPathGetBoundingBox( layer.path ).origin
有一件事我尝试过DID可以工作,但会导致其他地方出现问题:
编辑:一旦您自动旋转屏幕,这个BREAKS.我的猜测:Apple的自动旋转需要控制"转换"属性.
CAShapeLayer
CGPath
,将其添加到图层{{0,0},{0,0}}
layer.frame = CGPathGetBoundingBox( layer.path )
layer.transform = CGAffineTransformMakeTranslation( CGPathGetBoundingBox( layer.path ).origin.x * -1, // same for y-coord: set it to "-1 * the path's origin
这很有效,但是......许多第三方代码假定a的初始变换CALayer
是Identity.
应该不是这么难!当然,我在这里做错了什么?
(我有一个建议:"每次添加一个路径,手动运行一个自定义函数来移动所有点-1 * (top-left-point.x, top-left-point.y)
".再次,这是有效的 - 但它是非常复杂的)
小智 5
将layer.bounds设置为路径边界是正确的做法—您要使图层的局部坐标空间与路径匹配。但是您还需要设置图层的position属性,以将其移动到其上层的正确位置。
(设置.frame会转换为为您计算正确的.bounds和.position值的框架,但始终会使bounds.origin保持不变,当路径边界的原点为非零时,这不是您想要的。)
因此,假设您尚未更改anchorPoint的常规值(.5,.5),并希望将图层齐平到其上层的原点,则应该可以进行以下操作:
CGRect r = CGPathGetBoundingBox(layer.path);
layer.bounds = r;
layer.position = CGPointMake(r.size.width*.5, r.size.height*.5);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4117 次 |
最近记录: |