ios - 在 View 中重新定位 CGPath/UIBezierPath

标签 ios uibezierpath cgpath

是否可以在 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/

相关文章:

ios - 使用 Facebook 电子邮件登录未返回

ios - TableView 的 Swift MKMapItem 数据源不起作用

swift - iOS 用其他颜色填充 UIBezierPath 的一半而不使用 CAGradientLayer

ios - 如何在 iOS 中创建多行字符串或多行标签作为 CGPath?

ios - iOS 上的 mmap 有时返回 0xffffffff

ios - Xcode 未使用 XCFrameworks 找到架构 x86_64 的符号

ios - 如何在路径内部描画 uibezierpath 的边界?

ios - 在 Core Graphics 中创建一个带有多色线段的圆

iphone - 核心文本: Render to an Odd Shape

ios - 与CGPathCreateCopyByStrokingPath相反?