【问题标题】:Drawing in my custom view在我的自定义视图中绘图
【发布时间】:2012-08-22 13:16:10
【问题描述】:

我添加了一个自定义视图,并想绘制一个内部较小的圆、外部较大的圆和垂直线。我班级中的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];
}

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

【问题讨论】:

    标签: iphone objective-c xcode drawing


    【解决方案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)];
    

    【讨论】:

    • 该死,是的。伙计,我觉得自己很愚蠢哈哈。我现在应该去睡觉了。非常感谢。
    【解决方案2】:

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

    【讨论】:

    • 它是白色的,我什至将 UIColor 更改为黑色,但不起作用。
    【解决方案3】:

    试着改变这两行

    [xAxis setLineWidth:2];
    [[UIColor grayColor] setStroke];
    

    【讨论】:

    • 做了,还是画不出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多