繁体   English   中英

不使用-(void)drawRect:(CGRect)rect方法绘制线吗?

[英]Draw lines without the -(void)drawRect:(CGRect)rect method?

我想在UIView中创建一个图表。 像这样: 在此处输入图片说明

有没有比使用此方法更容易绘制线条的方法:

-(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);
}

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

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

您可以尝试使用UIBezierPath类:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM