2012-04-02 37 views
1

我正在用這個tutorial跟着Quartz做一個圖形。使用Quartz繪製圖形的流暢線條

這是我硬編碼類:

#import "GraphView.h" 

#define kGraphHeight 110 
#define kDefaultGraphWidth 900 
#define kOffsetX 0 
#define kStepX 50 
#define kStepY 50 
#define kOffsetY 10 
#define kGraphBottom 110 
#define kGraphTop 0 
#define kBarTop 10 
#define kBarWidth 40 
#define kCircleRadius 3 

@implementation GraphView 

float data[] = {0.7, 0.4, 0.9, 1.0, 0.2, 0.85, 0.11, 0.75, 0.53, 0.44, 0.88, 0.77, 0.99, 0.55}; 

- (void)drawLineGraphWithContext:(CGContextRef)ctx 
{ 
    CGContextSetLineWidth(ctx, 0.2); 
    CGContextSetStrokeColorWithColor(ctx, [[UIColor colorWithRed:1.0 green:0.5 blue:0 alpha:1.0] CGColor]); 

    int maxGraphHeight = kGraphHeight - kOffsetY; 

    CGContextBeginPath(ctx); 
    CGContextMoveToPoint(ctx, kOffsetX, kGraphHeight - maxGraphHeight * data[0]); 
    for (int i = 1; i < sizeof(data); i++) 
    { 
     CGContextAddLineToPoint(ctx, kOffsetX + i * kStepX, kGraphHeight - maxGraphHeight * data[i]); 
    } 
    CGContextDrawPath(ctx, kCGPathStroke); 
    CGContextSetFillColorWithColor(ctx, [[UIColor colorWithRed:1.0 green:0.5 blue:0 alpha:1.0] CGColor]); 
} 

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetAllowsAntialiasing(context, true); 
    for (int i = 0; i < sizeof(data); i++) 
    { 
     [self drawLineGraphWithContext:context]; 
    } 
} 

@end 

這是結果:

enter image description here

正如你所看到的,線不能有任何欠光滑。他們看起來不太好。嘗試抗鋸齒但沒有任何改變,我如何提高線條質量?

謝謝!

回答

3

你的線寬應該是2.0,而不是0.2。它很難爲你畫出一條體面的線條。

此外,它似乎是n次繪製圖,其中n是您的數據點數 - 您的drawRect和drawInContext方法中有for循環。

你不需要設置任何抗鋸齒,默認應該看你的權利。

+0

你是對的,我是如此f @ @#慚愧。我正在繪製和重繪,這就是爲什麼它看起來很糟糕。從drawRect中移除for循環做到了。 – ferostar 2012-04-02 22:41:15