简体   繁体   English

不使用-(void)drawRect:(CGRect)rect方法绘制线吗?

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

I want to make a chart inside an UIView. 我想在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. 您为什么不尝试UIBezierPath来自Apple的高级实现。

Simply refer UIBezierPath - Apple Doc for more info 只需参考UIBezierPath-Apple Doc了解更多信息

You can try using UIBezierPath class: 您可以尝试使用UIBezierPath类:

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. 如果需要绘制图表,请尝试使用CorePlot-绘图框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 -(void)drawRect:(CGRect)rect,绘制一个迷宫并检测其边界 - - (void)drawRect:(CGRect)rect , draw a maze and detect its boundary 如何调用 -(void)drawRect:(CGRect)rect - How to call -(void)drawRect:(CGRect)rect - (void)drawRect:(CGRect)rect; 正在耗尽几乎所有的iPhone CPU - -(void) drawRect:(CGRect)rect; is using up nearly all of iPhone CPU 如何启动函数 drawRect(rect: CGRect) ? 迅速 - How to start function drawRect(rect: CGRect) ? Swift 在drawRect:(CGrect)rect,objective中不显示阴影 - shadow does not show in drawRect:(CGrect)rect,objective setNeedsDisplay()没有调用draw(_ rect:CGRect) - setNeedsDisplay() is not calling draw(_ rect: CGRect) 使用CAGradientLayer和drawRect:(CGRect)绘制顺序 - Draw order using CAGradientLayer & drawRect:(CGRect) UITextField - (void)drawPlaceholderInRect:(CGRect)rect在iOS 7中返回不同的CGRect高度 - UITextField - (void)drawPlaceholderInRect:(CGRect)rect returns different CGRect height in iOS 7 为什么不调用 draw(_ layer: CALayer, in ctx: CGContext) 没有空 draw(_ rect: CGRect)? - Why draw(_ layer: CALayer, in ctx: CGContext) is not called without empty draw(_ rect: CGRect)? 覆盖drawRect:(CGRect)rect在Objective-C(iOS)中吗? - Overriding drawRect:(CGRect)rect in Objective-C (iOS)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM