簡體   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