贴一个绘制曲线的代码
- (void)drawRect:(CGRect)rect {
    // Drawing code
    // 绘制坐标轴
    [self drawAxic];
    // 绘制曲线
    [self drawCurve];
}
- (void) drawCurve {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);//线的宽度
    CGFloat with = [UIScreen mainScreen].bounds.size.width;
    CGFloat gap  = (with - 20)/19;
    // 生成点坐标
    CGPoint sPoints[20] ;
    for (int i = 0; i <20; i ++) {
        sPoints[i] = CGPointMake(10 + gap*i, [[self.yValuesArray objectAtIndex:i] integerValue]);
    }
    CGContextAddLines(context, sPoints, 20);//添加线
    CGContextSetRGBStrokeColor(context,0, 0, 1, 1); // 线条颜色
    CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
}
- (void) drawAxic {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context,1.5);//线的宽度
    CGContextSetRGBStrokeColor(context,0, 0, 0, 1);
    CGPoint yPoint[2];
    yPoint[0] = CGPointMake(10, 10);
    yPoint[1] = CGPointMake(10, 100);
    CGContextAddLines(context, yPoint, 2);//添加线
    CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGPoint xPoint[2];
    xPoint[0] = CGPointMake(10, 100);
    xPoint[1] = CGPointMake(width - 10, 100);
    CGContextAddLines(context, xPoint, 2);//添加线
    CGContextDrawPath(context, kCGPathStroke); //根据坐标绘制路径
}
可以通过动态调整 yValuesArray 数组,及调用 
[self setNeedsDisplay];
“` 
可以达到动态曲线图。
                    
                
                
            
        
浙公网安备 33010602011771号