设置新创建的CAShapeLayer的正确框架 [英] Setting correct frame of a newly created CAShapeLayer

查看:94
本文介绍了设置新创建的CAShapeLayer的正确框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之:


  1. Apple未设置框架或<$自动为 CAShapeLayer 设置c $ c>边界(Apple尚未实现等效于 [UIView sizeThatFits]

  2. 如果使用路径边界框的大小设置框架,则一切都会出错。无论您如何设置,它都会弄乱路径

  1. Apple does NOT set the frame or bounds for a CAShapeLayer automatically (and Apple HAS NOT implemented an equivalent of [UIView sizeThatFits])
  2. If you set the frame using the size of the bounding-box of the path ... everything goes wrong. No matter how you try to set it, it screws-up the path

所以,以编程方式设置框架的正确方法是什么一个新创建的 CAShapeLayer 和一个新添加的 CGPath ? Apple的文档对此事保持沉默。

So, what's the correct way to programmatically set the frame of a newly-created CAShapeLayer with a newly-added CGPath ? Apple's docs are silent on the matter.

我尝试过的方法不起作用:

Things I've tried, that don't work:


  1. 创建 CAShapeLayer

  2. 创建 CGPath ,将其添加到图层

  3. 检查图层的框架-它是 {{0,0},{ 0,0}}

  4. 设置: layer.frame = CGPathGetBoundingBox(layer.path)

  5. 框架现在是正确的,但是路径现在是双偏移量-更改框架会导致路径有效地移动了一个额外的位置(x,y)像素

  1. Create a CAShapeLayer
  2. Create a CGPath, add it to the layer
  3. Check the layer's frame - it's {{0,0},{0,0}}
  4. Set: layer.frame = CGPathGetBoundingBox( layer.path )
  5. Frame is now correct, but the path is now DOUBLE offset - changing the frame causes the path to effectively be shifted an extra (x,y) pixels

设置: layer.bounds = CGPathGetBoundingBox (layer.path)

我尝试过DID起作用的一件事,但在其他地方却引起了问题:

One thing I've tried that DID work, but causes problems elsewhere:

编辑:此功能会在您自动旋转屏幕时立即中断。我的猜测:Apple的自动旋转需要控制 transform属性。

this BREAKS as soon as you auto-rotate the screen. My guess: Apple's auto-rotate requires control of the "transform" property.


  1. 创建 CAShapeLayer

  2. 创建 CGPath ,并将其添加到图层中

  3. 检查图层的框架- {{0,0},{0,0}}

  4. 设置: layer.frame = CGPathGetBoundingBox(layer.path)

  5. 设置: layer.transform = CGAffineTransformMakeTranslation(CGPathGetBoundingBox( layer.path).origin.x * -1,//与y坐标相同:将其设置为 -1 *路径的原点

  1. Create a CAShapeLayer
  2. Create a CGPath, add it to the layer
  3. Check the layer's frame - it's {{0,0},{0,0}}
  4. Set: layer.frame = CGPathGetBoundingBox( layer.path )
  5. Set: layer.transform = CGAffineTransformMakeTranslation( CGPathGetBoundingBox( layer.path ).origin.x * -1, // same for y-coord: set it to "-1 * the path's origin

这是有效的,但是...很多第三方代码都假设 CALayer 的初始转换是Identity。

This works, but ... lots of 3rd party code assumes that the initial transform for a CALayer is Identity.

应该不难!在这里肯定我做错了什么?

It shouldn't be this difficult! Surely there's something I'm doing wrong here?

(我有一个建议:每次添加路径时,请手动运行自定义函数以将所有点移动 -1 *(top-left-point.x,top-left-point.y)。同样, Orks-但它非常复杂)

(I've had one suggestion: "every time you add a path, manually run a custom function to shift all the points by -1 * (top-left-point.x, top-left-point.y)". Again, that works - but it's ridiculously complex)

推荐答案

设置layer.bounds到路径界限是正确的做法-您想使图层的局部坐标空间与路径匹配。但是,然后您还需要设置图层的position属性,以将其移动到其上层的正确位置。

Setting layer.bounds to the path bounds is the right thing to do — you want to make the layer's local coordinate space match the path. But you then also need to set the layer's position property to move it into the right place in its superlayer.

(设置.frame会转换为计算正确的值的框架.bounds和.position,但它始终使bounds.origin保持不变,当路径边界的原点为非零时,这不是您想要的。)

(Setting .frame translates into the framework computing the right values of .bounds and .position for you, but it always leaves bounds.origin untouched, which is not what you want when your path bounds has a non-zero origin.)

因此,假设您没有更改anchorPoint的常规值(.5,.5)并想将图层齐平到其上层的原点,这样的事情应该起作用:

So something like this should work, assuming you haven't changed the anchorPoint from its usual (.5, .5) value and want to position the layer flush to the origin of its superlayer:

CGRect r = CGPathGetBoundingBox(layer.path);
layer.bounds = r;
layer.position = CGPointMake(r.size.width*.5, r.size.height*.5);

这篇关于设置新创建的CAShapeLayer的正确框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆