Search code examples
objective-cscalecgaffinetransformuibezierpath

Dynamically scale and translate shape


I am drawing a series of UIBezierPath's, all of which have different sizes and locations. I would like to scale each shape to a specific size and move it to a specific location on a UIView. I am working with CGAffineTransformation right now. It is quickly becoming messy, as the location and size do not work for all the shapes.

Is there any way I can scale a UIBezierPath to a specific size and translate (move) it to a specific location that will work for most shapes? Maybe there is a way to set the size and location of a shape without having to use transformations that I am unaware of? The shapes I am drawing are all very different in size, some with jagged edges.

This is what I am doing so far with my AffineTransformations.

CGAffineTransform scale = CGAffineTransformMakeScale(2.0f,2.0f); 
[path applyTransform:scale];

CGAffineTransform move = CGAffineTransformMakeTranslation(-10, -10);
[path applyTransform:move];

Where path is a UIBezierPath.

Is that correct? The values I am using (-10, 2.0f) are just examples.


Solution

  • I think the best way to solve this problem is to convert the path into a CAShapeLayer and set the position and size of the layer. Very simple and elegant. However, there are some bugs that you will have to resolve depending on the implementation.

    myCAShapeLayer.path = myUIBezierPath.path;
    myCAShapeLayer.position = CGRectMake(x, y);
    

    CALayers are awesome!