简体   繁体   中英

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

I want to make a chart inside an UIView. Something like this: 在此处输入图片说明

Is there an easier way to draw the lines than using this method:

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

?

Why don't you try UIBezierPath a high-level implementation from Apple.

Simply refer UIBezierPath - Apple Doc for more info

You can try using UIBezierPath class:

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

If you need to draw a chart, try to use CorePlot - plotting framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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