如何调整和翻译CAShapeLayer?

时间:2014-11-08 01:29:34

标签: objective-c scale uibezierpath cashapelayer

我有一个从UIBezierPath中提取的CAShapeLayer但由于某种原因,在bezier上应用的平移和缩放在绘制时没有转移到CAShapeLayer。有没有办法对它应用相同的转换? 这是我的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);

这是我的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];

1 个答案:

答案 0 :(得分:4)

这不是你怎么做的, 你需要将变换应​​用于CAShapeLayer本身。

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;