Rearrange CGPath / UIBezierPath in a View

Can I move an already drawn CGPath / UIBezierPath to a view? I would like to move or reposition the path, and then perhaps recall the drawinRect method to just show the drawing again.

+8
ios uibezierpath cgpath
source share
1 answer

From your question, it seems that you are drawing a path using Core Graphics inside drawRect: (compared to using CAShapeLayer ), so I will explain the version first.

Moving CGPath

You can create a new path by changing a different path. The translation transform moves the transformed object a certain distance in x and y. Thus, using the translation transform, you can transfer the existing path to a certain number of points in both x and y.

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

Then you can use movedPath to paint just like you already do.

You can also change the same path

 yourPath = CGPathCreateCopyByTransformingPath(yourPath, &translation); 

and just redraw it.

Moving a shape layer

If you use a shape layer, moving is even easier. Then you only need to reposition the layer using the position property.

Update:

If you want to use a shape layer, you can simply create a new CAShapeLayer and set its path to your CGPath. For this, you will need QuartzCore.framework, 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 your position.

 shape.position = thePointYouWantToMoveTheShapeTo; 
+19
source share

All Articles