2012-04-02 35 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