How do I resize and translate CAShapeLayer?
I have a CAShapeLayer derived from UIBezierPath, but for some reason the translation and scale applied in the bezier did not carry over to the CAShapeLayer when it was drawn. Is there a way to apply the same transforms to it? Here is the code for my Bezier:
//// General Declarations
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 1];
///Where the Transformations are
CGContextTranslateCTM(context, 52.7, 45.46);
CGContextScaleCTM(context, 0.5, 0.5);
UIBezierPath *trackPath = UIBezierPath.bezierPath;
[trackPath moveToPoint: CGPointMake(-28.3, 160.72)];
[trackPath addCurveToPoint: CGPointMake(125.37, -41.18) controlPoint1: CGPointMake(-28.3, 160.72) controlPoint2: CGPointMake(-11.62, -41.18)];
[trackPath addCurveToPoint: CGPointMake(279.04, 160.72) controlPoint1: CGPointMake(262.36, -41.18) controlPoint2: CGPointMake(279.04, 160.72)];
[trackPath moveToPoint: CGPointMake(279.04, 160.82)];
[trackPath addCurveToPoint: CGPointMake(432.7, 362.72) controlPoint1: CGPointMake(279.04, 160.82) controlPoint2: CGPointMake(295.72, 362.72)];
[trackPath moveToPoint: CGPointMake(-28.76, 160.82)];
[trackPath addCurveToPoint: CGPointMake(-182.43, 362.72) controlPoint1: CGPointMake(-28.76, 160.82) controlPoint2: CGPointMake(-45.45, 362.72)];
CGContextSaveGState(context);
[color setStroke];
trackPath.lineWidth = 1;
[trackPath stroke];
CGContextDrawPath(context, kCGPathFill);
CGContextRestoreGState(context);
And this is my CAShapeLayer
raceTrack = [CAShapeLayer layer];
raceTrack.path = trackPath.CGPath;
raceTrack.strokeColor = [UIColor whiteColor].CGColor;
raceTrack.fillColor = [UIColor clearColor].CGColor;
raceTrack.lineWidth = 1;
[self.layer addSublayer:raceTrack];
+3
source to share
1 answer
This is not how you do it, you need to apply the transform to the CAShapeLayer itself.
CATransform3D translate = CATransform3DMakeTranslation(52.7, 45.46, 0);
CATransform3D scale = CATransform3DMakeScale(0.5, 0.5, 1);
CATransform3D transform = CATransform3DConcat(translate, scale);
CAShapeLayer *raceTrack = [CAShapeLayer layer];
raceTrack.transform = transform;
+4
source to share