是否可以在 View 上重新定位已绘制的 CGPath/UIBezierPath?我想移动或更改路径的位置,然后可能会调用 drawinRect 方法再次显示绘图。
最佳答案
从你的问题来看,你似乎是在 drawRect:
中使用 Core Graphics 绘制路径(与使用 CAShapeLayer
相比)所以我会先解释一下版本.
移动 CGPath
您可以通过转换另一条路径来创建一条新路径。平移变换将变换后的对象在 x 和 y 中移动一定距离。因此,使用平移变换,您可以将现有路径在 x 和 y 方向移动一定数量的点。
CGAffineTransform translation = CGAffineTransformMakeTranslation(xPixelsToMove,
yPixelsToMove);
CGPathRef movedPath = CGPathCreateCopyByTransformingPath(originalCGPath,
&translation);
然后您可以使用 movedPath
以与您已经在做的相同的方式进行绘制。
你也可以更改修改相同的路径
yourPath = CGPathCreateCopyByTransformingPath(yourPath,
&translation);
然后简单地重新绘制它。
移动形状图层
如果您使用的是形状图层,移动它会更容易。然后您只需使用 position
属性更改层的位置。
更新:
如果你想使用形状图层,你可以简单地创建一个新的 CAShapeLayer 并将它的路径设置为你的 CGPath。由于 CAShapeLayer 是核心动画的一部分,因此您需要 QuartzCore.framework。
CAShapeLayer *shape = [CAShapeLayer layer];
shape.path = yourCGParth;
shape.fillColor = [UIColor redColor].CGColor;
[someView.layer addSublayer:shape];
然后要移动形状,只需更改其位置即可。
shape.position = thePointYouWantToMoveTheShapeTo;
关于ios - 在 View 中重新定位 CGPath/UIBezierPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17168931/