CGMutablePath始终从0,0开始

时间:2015-09-15 13:40:06

标签: ios uiview quartz-graphics uibezierpath

学习使用UIBezierPath路径。我可以在我的UIView中画一条简单的线但是原点总是0,0。我错过了什么?

简单代码:

CGMutablePathRef cgPath = CGPathCreateMutable();

UIBezierPath *aPath = [UIBezierPath bezierPath];

CGPathMoveToPoint(cgPath, NULL, 100, 100);
CGPathAddLineToPoint(cgPath, NULL, 200,200);

aPath.CGPath = cgPath;
aPath.usesEvenOddFillRule = YES;

CAShapeLayer *lines = [CAShapeLayer layer];
lines.path = aPath.CGPath;
lines.bounds = CGPathGetBoundingBox(lines.path);
lines.strokeColor = [UIColor whiteColor].CGColor;
lines.fillColor = [UIColor clearColor].CGColor; /*if you just want lines*/
lines.lineWidth = 3;

[self.UIView.layer addSublayer:lines];

CGPathRelease(cgPath);

我将起点设置为100,100并绘制一条线到200,200。但它总是从0,0左右开始。

更新的代码 - 但是我还在UIBezierPath和CGPath之间混音吗?

static CGFloat thickness = 0.5;
static CGFloat shapeSizex = 300;
static CGFloat shapeSizey = 150;
static CGFloat xanchor = 20; // bottom-left corner x
static CGFloat yanchor = 180; // bottom-left corner y

UIBezierPath *axisPath = [UIBezierPath bezierPath];
[axisPath moveToPoint:CGPointMake(xanchor, yanchor-shapeSizey)];
[axisPath addLineToPoint:CGPointMake(xanchor, yanchor)];
[axisPath addLineToPoint:CGPointMake(xanchor+shapeSizex, yanchor)];

CAShapeLayer *lines = [CAShapeLayer layer];
lines.path = axisPath.CGPath;
lines.strokeColor = [UIColor whiteColor].CGColor;
lines.lineWidth = thickness;

[self.ChartViewOutlet.layer addSublayer:lines];

它做了我想要的绘制图形轴。

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

lines.bounds = CGPathGetBoundingBox(lines.path);

您重新定义了图层以环绕路径。因此,当您绘制它时,它会被转换为将绘制路径的中心放在图层的中心。