在视图中重新定位CGPath / UIBezierPath

是否有可能重新定位已经绘制的CGPath / UIBezierPath在视图上? 我想移动或改变一个path的位置,那么也许回想一下drawinRect方法再次显示绘图。

从你的问题,它听起来像你正在drawRect:使用核心graphics绘制pathdrawRect:与使用CAShapeLayer相比),所以我会解释的版本第一。

移动一个CGPath

您可以通过转换另一个path来创build一个新的path。 平移变换将变换的对象在x和y中移动一定的距离。 因此,使用平移变换,可以将现有path在x和y中移动一定数量的点。

 CGAffineTransform translation = CGAffineTransformMakeTranslation(xPixelsToMove, yPixelsToMove); CGPathRef movedPath = CGPathCreateCopyByTransformingPath(originalCGPath, &translation); 

然后你可以使用移动的movedPath来绘制你已经做的相同的方式。

你也可以修改相同的path

 yourPath = CGPathCreateCopyByTransformingPath(yourPath, &translation); 

并简单地重绘它。

移动一个形状图层

如果你正在使用一个形状图层,移动它更容易。 那么你只需要使用position属性来改变图层的position

更新:

如果要使用形状图层,只需创build一个新的CAShapeLayer并将其path设置为您的CGPath即可。 你需要QuartzCore.framework,因为CAShapeLayer是Core Animation的一部分。

 CAShapeLayer *shape = [CAShapeLayer layer]; shape.path = yourCGParth; shape.fillColor = [UIColor redColor].CGColor; [someView.layer addSublayer:shape]; 

然后移动形状,你只需改变它的位置。

 shape.position = thePointYouWantToMoveTheShapeTo;