在视图中重新定位CGPath / UIBezierPath [英] Reposition CGPath/UIBezierPath in View
问题描述
是否可以在视图上重新定位已绘制的CGPath / UIBezierPath?我想移动或更改路径的位置然后可能回想起drawinRect方法再次显示图形。
Is it possible to reposition an already drawn CGPath/UIBezierPath on a view? I would like to move or change a path's position then perhaps recall the drawinRect method to just show the drawing again.
推荐答案
从你的问题听起来你正在使用核心图形在 drawRect:
中绘制路径(与使用 CAShapeLayer
相比)我将首先解释该版本。
From your question it sounds like you are drawing the path using Core Graphics inside drawRect:
(as compared to using a CAShapeLayer
) so I'll explain the version first.
您可以通过转换另一个来创建新路径路径。平移变换将变换后的对象在x和y中移动一定距离。因此,使用平移变换,您可以在x和y中移动现有路径一定数量的点。
You can create a new path by transforming another path. A translation transform moves the transformed object a certain distance in x and y. So using a translation transform you can move your existing path a certain number of points in both x and y.
CGAffineTransform translation = CGAffineTransformMakeTranslation(xPixelsToMove,
yPixelsToMove);
CGPathRef movedPath = CGPathCreateCopyByTransformingPath(originalCGPath,
&translation);
然后你可以使用 movedPath
来绘制就像你现在一样。
Then you could use the movedPath
to draw the same way you are already doing.
你也可以改变修改相同的路径
You could also change modify the same path
yourPath = CGPathCreateCopyByTransformingPath(yourPath,
&translation);
并简单地重新绘制。
如果使用形状图层,移动它会更容易。然后,您只需使用位置
属性更改图层的位置。
If you are using a shape layer, moving it is even easier. Then you only have to change the position of the layer using the position
property.
如果你想使用形状图层,你可以简单地创建一个新的CAShapeLayer并将其路径设置为你的CGPath。你需要QuartzCore.framework,因为CAShapeLayer是Core Animation的一部分。
If you want to use a shape layer you can simply create a new CAShapeLayer and set its path to be your CGPath. You will need QuartzCore.framework for this since CAShapeLayer is part of Core Animation.
CAShapeLayer *shape = [CAShapeLayer layer];
shape.path = yourCGParth;
shape.fillColor = [UIColor redColor].CGColor;
[someView.layer addSublayer:shape];
然后移动形状你只需改变它的位置。
Then to move the shape you simply change its position.
shape.position = thePointYouWantToMoveTheShapeTo;
这篇关于在视图中重新定位CGPath / UIBezierPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!