3

I have a CAShapeLayer which contains a CGMutablePath that has a stroke drawn around it. In my app, I transform this CAShapeLayer to increase / decrease it's size at certain times. I'm noticing when I transform the CAShapeLayer, the stroke gets transformed as well. Ideally I'd like to keep the lineWidth of the stroke at 3 at all times even when the CAShapeLayers transformed.

I tried shutting off the stroke before I transformed then readding it afterwards but it didn't work:

subLayerShapeLayer.lineWidth = 0;
subLayerShapeLayer.strokeColor = nil;
self.layer.sublayerTransform = CATransform3DScale(self.layer.sublayerTransform, graphicSize.width / self.graphic.size.width, graphicSize.height / self.graphic.size.height, 1);
shapeLayer.strokeColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor;;
shapeLayer.lineWidth = 3;

Does anyone know how I might be able to accomplish this task? Seems as though it should be able to redraw the stroke after transforming somehow.

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169

1 Answers1

3

Transform the CGPath itself and not its drawn representation (the CAShapeLayer).

Have a close look at CGPathCreateMutableCopyByTransformingPath - CGPath Reference

CGPathCreateMutableCopyByTransformingPath

Creates a mutable copy of a graphics path transformed by a transformation matrix.

CGMutablePathRef CGPathCreateMutableCopyByTransformingPath(
   CGPathRef path,
   const CGAffineTransform *transform
);

Parameters

path The path to copy.

transform A pointer to an affine transformation matrix, or NULL if no transformation is needed. If specified, Quartz applies the transformation to all elements of the new path. Return Value A new, mutable copy of the specified path transformed by the transform parameter. You are responsible for releasing this object.

Availability Available in iOS 5.0 and later. Declared In CGPath.h

Till
  • 27,559
  • 13
  • 88
  • 122
  • Thanks for the response. Won't this take away some of the benefits of using a CAShapeLayer if I'm just replacing it's path every time it gets transformed? Wouldn't it be the same as just drawing to the context in drawRect? – Ser Pounce Dec 25 '12 at 19:58
  • I am sorry but I currently have no better solution for you at hand - I wouldnt know any other way to achieve that effect. – Till Dec 25 '12 at 20:01