1

我添加了一个自定义视图,并想绘制一个内部较小的圆圈、外部较大的圆圈和垂直线。drawRect:我班上的方法是这样的。

- (void) drawRect:(CGRect)rect{
    CGContextRef myContext = UIGraphicsGetCurrentContext();
    float height = rect.size.height;
    float width = rect.size.width;
    CGContextTranslateCTM(myContext, 0.0, height);
    CGContextScaleCTM(myContext, 1.0, -1.0);
    CGPoint middle = CGPointMake(width/2, height/2);
    UIBezierPath *innerCirclePath = [UIBezierPath bezierPathWithArcCenter:middle radius:25 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:YES];
    [innerCirclePath setLineWidth:2];
    [[UIColor redColor] setStroke];
    [innerCirclePath stroke];
    UIBezierPath *outerCirclePath = [UIBezierPath bezierPathWithArcCenter:middle radius:120 startAngle:0 endAngle:DEGREES_TO_RADIANS(360) clockwise:NO];
    [outerCirclePath setLineWidth:2];
    [[UIColor greenColor] setStroke];
    [outerCirclePath stroke];
    UIBezierPath *xAxis = [UIBezierPath bezierPath];
    [xAxis moveToPoint:CGPointMake(width, height/2 - 150)];
    [xAxis addLineToPoint:CGPointMake(width, height/2 + 150)];
    [xAxis closePath];
    [[UIColor grayColor] setStroke];
    [xAxis setLineWidth:2];
    [xAxis stroke];
}

现在我确实得到了外圈和内圈,但没有我想要的垂直线。为什么不被绘制?

4

3 回答 3

1

您正在使用这两行来设置“垂直线”的路径:

[xAxis moveToPoint:CGPointMake(width, height/2 - 150)];
[xAxis addLineToPoint:CGPointMake(width, height/2 + 150)];

问题是两个点的 X 坐标都是视图的右边缘。也许您希望它位于视图的中心:

CGFloat xMid = CGRectGetMidX(rect);
[xAxis moveToPoint:CGPointMake(xMid, height/2 - 150)];
[xAxis addLineToPoint:CGPointMake(xMid, height/2 + 150)];
于 2012-08-22T05:16:10.120 回答
0

你视图的背景是什么颜色的?你应该确保它不是太靠近,greyColor否则即使它在那里你也不会看到它。

于 2012-08-22T05:12:28.037 回答
0

尝试改变这两行

[xAxis setLineWidth:2];
[[UIColor grayColor] setStroke];
于 2012-08-22T05:13:36.490 回答