画线没有 - (void)drawRect:(CGRect)rect方法?

时间:2014-08-28 11:31:12

标签: ios iphone xcode uiview

我想在UIView中制作一个图表。像这样的东西: enter image description here

绘制线条的方法比使用此方法更简单:

-(void)drawRect:(CGRect)rect{

    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0f);

    CGContextMoveToPoint(context, 0.0f, 0.0f); //start at this point

    CGContextAddLineToPoint(context, 20.0f, 20.0f); //draw to this point

    // and now draw the Path!
    CGContextStrokePath(context);
}

2 个答案:

答案 0 :(得分:0)

为什么不尝试UIBezierPath Apple的高级实现。

只需参阅UIBezierPath - Apple Doc了解更多信息

答案 1 :(得分:0)

您可以尝试使用UIBezierPath类:

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:startPoint];
[path addLineToPoint:endPoint];
[path stroke];

如果您需要绘制图表,请尝试使用CorePlot - 绘图框架。

相关问题